Interface in java and its rules

In Java, an interface is a reference type, similar to a class, that defines a set of abstract methods that must be implemented by any class that implements the interface. Interfaces provide a way to achieve abstraction and multiple inheritance of types, allowing classes to share common functionality without sharing a common implementation. Here are the key rules and characteristics of interfaces in Java:

  1. Declaration:
    • An interface is declared using the interface keyword followed by the interface name.
    • Interface names typically start with an uppercase letter and follow the Java naming conventions.
  2. Abstract Methods:
    • An interface can contain abstract methods, which are method declarations without a body. These methods are implicitly public and abstract.
    • All methods in an interface are implicitly public and abstract, even if these modifiers are not explicitly specified.
  3. Constants:
    • Interfaces can also contain constant variables, which are implicitly public, static, and final.
    • Constant variables must be initialized at the time of declaration and cannot be modified by implementing classes.
  4. Extending Interfaces:
    • An interface can extend one or more other interfaces using the extends keyword.
    • If an interface extends multiple interfaces, it inherits all abstract methods and constant variables from all the extended interfaces.
  5. Implementing Interfaces:
    • A class implements an interface by providing concrete implementations for all the abstract methods declared in the interface.
    • A class can implement multiple interfaces, separated by commas, allowing it to inherit behavior from multiple sources.
  6. Default Methods (Introduced in Java 8):
    • Java 8 introduced the concept of default methods in interfaces, allowing interfaces to provide default implementations for methods.
    • Default methods are declared using the default keyword and can have a body. They are used to provide backward compatibility when new methods are added to existing interfaces.
  7. Static Methods (Introduced in Java 8):
    • Java 8 also introduced static methods in interfaces, which are declared using the static keyword.
    • Static methods in interfaces provide utility methods that are associated with the interface itself, rather than with instances of implementing classes.
  8. Marker Interfaces:
    • Interfaces with no methods, known as marker interfaces, are used to mark classes with certain properties.
    • Examples include Serializable, Cloneable, and Remote interfaces in Java.

Here’s a simple example demonstrating the declaration and implementation of an interface:

// Interface declaration
interface Animal {
    void eat(); // Abstract method
    void sleep(); // Abstract method
    // Constant variable
    String TYPE = "Animal";
}
// Class implementing the interface
class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }
    @Override
    public void sleep() {
        System.out.println("Dog is sleeping");
    }
}
public class Main {
    public static void main(String[] args) {
        // Creating an object of class Dog
        Dog dog = new Dog();
        // Calling methods defined in the interface
        dog.eat();
        dog.sleep();
        // Accessing constant variable from the interface
        System.out.println("Type of animal: " + Animal.TYPE);
    }
}

In this example:

  • Animal is an interface containing two abstract methods (eat() and sleep()) and a constant variable (TYPE).
  • Dog is a class that implements the Animal interface, providing concrete implementations for the abstract methods.
  • The main method demonstrates creating an object of the Dog class and calling methods defined in the Animal interface, as well as accessing the constant variable TYPE.

One response to “Interface in java and its rules”

  1. Happy coding..

    Like

Leave a comment