9. Interface in Java With Examples
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors.
Example:
interface Animal { void sound(); } class Dog implements Animal { public void sound() { System.out.println("Dog barks"); } } class Cat implements Animal { public void sound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.sound(); cat.sound(); } }
10. Marker Interface in Java
A marker interface is an interface with no methods or fields. It is used to signal to the JVM or other code that the class implementing the interface has a special property.
Example:
import java.io.Serializable; class Student implements Serializable { private String name; private int age; // Constructor, getters, and setters }
11. Difference Between Abstract Class And Interface in Java
Abstract classes can have both abstract and concrete methods, while interfaces can only have abstract methods (until Java 8, which introduced default and static methods). Abstract classes can have instance variables, constructors, and can provide method implementations.
Example:
abstract class Animal { abstract void sound(); void sleep() { System.out.println("Animal is sleeping"); } } interface AnimalInterface { void sound(); } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } class Cat implements AnimalInterface { public void sound() { System.out.println("Cat meows"); } }
12. Interface Default Methods in Java
Default methods in interfaces allow you to add new methods to interfaces without breaking the existing code that implements the interface. They are defined using the default
keyword.
Example:
interface Animal { void sound(); default void sleep() { System.out.println("Animal is sleeping"); } } class Dog implements Animal { public void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); dog.sleep(); } }
13. Interface Static Methods in Java
Static methods in interfaces are similar to static methods in classes. They belong to the interface and can be called without an instance of the interface.
Example:
interface MathUtils { static int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { int result = MathUtils.add(5, 3); System.out.println("Result: " + result); } }
14. Private Methods in Java Interface
Private methods in interfaces are used to share common code between default methods. They cannot be accessed outside the interface.
Example:
interface Animal { void sound(); default void sleep() { System.out.println("Animal is sleeping"); log("Sleeping"); } private void log(String message) { System.out.println("Log: " + message); } } class Dog implements Animal { public void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); dog.sleep(); } }