Interfaces in Java


An interface in Java is a collection of abstract methods. It is a way to achieve abstraction and multiple inheritance in Java, as a class can implement multiple interfaces. Interfaces provide a contract for classes that implement them, specifying a set of methods that the implementing class must define. They are declared using the "interface" keyword.

Why Use Interfaces:

  • Abstraction: Interfaces provide a level of abstraction, allowing you to define a set of methods without specifying their implementation.
  • Multiple Inheritance: Java classes can implement multiple interfaces, addressing the limitation of single inheritance in class-based languages.
  • Loose Coupling: Interfaces allow loose coupling between classes, promoting flexibility and ease of maintenance.
  • Contract Specification: Defines a contract that implementing classes must adhere to, ensuring a consistent API.

Creating and Using Interfaces:


//Declaration of interface
interface MyInterface {
    void method1();
    int method2();
}

// Implementation of the interface
class MyClass implements MyInterface {
    public void method1() {
        // Implementation
    }

    public int method2() {
        // Implementation
        return 0;
    }
}

Restrictions on Interfaces:

  • Interfaces cannot have instance variables other than public static final variables (constants).
  • They cannot have constructors.
  • Interface methods cannot be private or have body implementation before Java 9.
  • Default methods cannot override methods from Object class, as they are already implemented.
  • Static methods in interfaces cannot be overridden in implementing classes.
  • Interfaces cannot be marked as final or abstract.
  • Constants in interfaces should be initialized.
  • Interface methods cannot be marked as final or static.
  • Interfaces cannot have access modifiers for methods; they are always public.
  • Interfaces cannot extend a class; they can only extend other interfaces.
  • An interface cannot implement another interface.
  • Nested interfaces must be declared as static.
  • Abstract classes can have constructors, but interfaces cannot.
  • Interface methods cannot throw broader exceptions than the overridden methods in implementing classes.
  • An interface can extend multiple interfaces using the extends keyword.
  • Interface methods cannot be marked as final.
  • All variables in an interface are implicitly public, static, and final.
  • Abstract classes can have both abstract and concrete methods, but interfaces can only have abstract methods.
  • Interfaces cannot contain non-public methods.
  • Interfaces cannot contain non-abstract methods.
  • No Constructors: Interfaces cannot have constructors.
  • No Instance Variables: Interfaces can have only public static final variables (constants).
  • Default and Static Methods: Introduced in Java 8, allowing for method implementation in interfaces. etc...
Types of Interfaces in Java:

1. Normal Interfaces :  

A normal interface in Java contains abstract methods without any default or static methods.

Example:

interface MyInterface {
    void method1();
    int method2();
}

Features of Normal Interface :

  • Provides a pure abstraction by declaring abstract methods.
  • Requires implementing classes to provide concrete implementations. etc...
Restriction on Normal Interfaces:
  • Cannot have default or static methods.
  • Only abstract methods are allowed.
  • Cannot contain implementation for any method.
  • Cannot have constants other than public static final variables.
  • Cannot have constructors. etc..
2. Functional Interfaces : 

↪Has exactly one abstract method and may contain multiple default or static methods. Can be annotated with @FunctionalInterface.

Example:

@FunctionalInterface
interface MyFunctionalInterface {
    void singleAbstractMethod();
   
    default void defaultMethod() {
        // Implementation
    }
   
    static void staticMethod() {
        // Implementation
    }
}


Features of Functional Interface:
  • Supports the concept of functional programming.
  • Enables the use of lambda expressions.
  • Promotes the development of more expressive and concise code. etc..
Restrictions on Functional Interface:
  • Can have only one abstract method.
  • Can contain multiple default or static methods.
  • Cannot have more than one abstract method if annotated with @FunctionalInterface.
  • Abstract methods cannot have the same signature as default methods
3. Marker Interface:

↪A marker interface has no abstract methods and is often used for tagging or metadata purposes. Contains no methods (e.g., Serializable, Cloneable).

Example:
    1)

  1. interface MarkerInterface {
        // No methods allowed
    }

  2. 2) 

  1. // Marker Interface
    interface SerializableMarker {
        // No methods are defined
    }

    // A class implementing the marker interface
    class DataObject implements SerializableMarker {
        // Class implementation
        private String data;

        public DataObject(String data) {
            this.data = data;
        }

        // Other methods and behavior specific to DataObject
    }

    In this example, the SerializableMarker interface serves as a marker for classes that implement it. The DataObject class implements the SerializableMarker interface, indicating that objects of this class can be serialized. The marker interface itself does not contain any methods, but its presence is used to convey information about the class's capabilities or characteristics.

    Features of Marker Interface :
  • Used for tagging or marking classes.
  • Provides metadata about classes.
  • Often used for certain special treatment by frameworks or APIs. etc...
Restrictions on Marker Interface:
  • Cannot contain any methods (abstract, default, or static).
  • Lacks the ability to provide behavior or implementation. etc...

Key Points to Remember about Interfaces:

  • Interfaces provide a blueprint for classes to follow.
  • They support the concept of multiple inheritance.
  • Interface methods are implicitly public and abstract.
  • Interfaces can be used to achieve polymorphism.
  • Default methods were introduced to maintain backward compatibility when adding new methods to interfaces.
  • Interfaces promote code reusability.
  • A class can implement multiple interfaces, but it can extend only one class.
  • Interfaces are implicitly abstract, even if not explicitly declared.
  • All variables in an interface are implicitly public, static, and final.
  • Interfaces can extend other interfaces using the extends keyword.
  • In Java, interfaces are reference types.
  • Abstract classes can also implement interfaces.
  • An interface reference variable can hold any object of a class that implements the interface.
  • An interface cannot be instantiated; it requires a class to provide an implementation.
  • Interfaces can extend more than one interface, providing a form of multiple inheritance.
  • The instanceof operator can be used to check if an object implements a particular interface.
  • Interfaces support the concept of loose coupling.
  • Interfaces can be used in design patterns like the Adapter pattern.
  • Interfaces help achieve separation of concerns.
  • Functional interfaces can be used with lambda expressions in Java. etc...

Restrictions on Interfaces: 

  • Interfaces cannot have instance variables other than public static final variables (constants).
  • They cannot have constructors.
  • Interface methods cannot be private or have body implementation before Java 9.
  • Default methods cannot override methods from Object class, as they are already implemented.
  • Static methods in interfaces cannot be overridden in implementing classes.
  • Interfaces cannot be marked as final or abstract.
  • Constants in interfaces should be initialized.
  • Interface methods cannot be marked as final or static.
  • Interfaces cannot have access modifiers for methods; they are always public.
  • Interfaces cannot extend a class; they can only extend other interfaces.
  • An interface cannot implement another interface.
  • Nested interfaces must be declared as static.
  • Abstract classes can have constructors, but interfaces cannot.
  • Interface methods cannot throw broader exceptions than the overridden methods in implementing classes.
  • An interface can extend multiple interfaces using the extends keyword.
  • Interface methods cannot be marked as final.
  • All variables in an interface are implicitly public, static, and final.
  • Abstract classes can have both abstract and concrete methods, but interfaces can only have abstract methods.
  • Interfaces cannot contain non-public methods.
  • Interfaces cannot contain non-abstract methods. etc..

Interview Points - Key Questions:

  • What is an interface in Java?
  • Why do we need interfaces?
  • How do you create an interface?
  • Can an interface have constructors?
  • What is a functional interface?
  • What is a marker interface?
  • How is multiple inheritance achieved using interfaces?
  • Can an interface extend another interface?
  • What is the purpose of @FunctionalInterface?
  • How are interfaces different from abstract classes?
  • How do interfaces achieve abstraction?

Interview Questions and Answers:


Q. What is an interface in Java?

↪An interface in Java is a collection of abstract methods.

Q. How many programming languages support interfaces?

↪Java primarily supports interfaces, but other languages like C#, Swift, and Kotlin also have similar concepts.

Q. Why do we need interfaces in Java?

↪ Interfaces provide abstraction, support multiple inheritance, and promote loose coupling.

Q. How do you create an interface in Java?

↪Use the interface keyword, followed by the interface name and method signatures.

Q. Can an interface have constructors?

↪No, interfaces cannot have constructors.

Q. What is a functional interface?

↪A functional interface has exactly one abstract method and may contain multiple default or static methods.

Q. What is a marker interface?

↪A marker interface has no methods, often used for tagging or metadata purposes.

Q. What are default methods in interfaces?

↪Default methods are methods with a default implementation in interfaces, introduced in Java 8.

Q. Can an interface have instance variables?

↪No, interfaces can only have public static final variables (constants).

Q. How is multiple inheritance achieved in Java using interfaces?

↪A class can implement multiple interfaces, allowing it to inherit from multiple sources.

Q. What is the purpose of a marker interface?

↪A marker interface is used to tag classes for special treatment or to provide metadata.

Q. Can an interface extend another interface?

↪Yes, an interface can extend multiple interfaces using the extends keyword.

Q. What is the significance of the @FunctionalInterface annotation?

↪It indicates that the interface is intended to be a functional interface, and the compiler will enforce having only one abstract method.

Q. How are interfaces different from abstract classes?

↪Interfaces can only have abstract methods, while abstract classes can have a mix of abstract and concrete methods.

Q. How do you achieve abstraction using interfaces? 

↪By declaring abstract methods in interfaces, the implementation details are left to the classes that implement the interface.

Comments