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:
- Declaration:
- An interface is declared using the
interfacekeyword followed by the interface name. - Interface names typically start with an uppercase letter and follow the Java naming conventions.
- An interface is declared using the
- Abstract Methods:
- An interface can contain abstract methods, which are method declarations without a body. These methods are implicitly
publicandabstract. - All methods in an interface are implicitly
publicandabstract, even if these modifiers are not explicitly specified.
- An interface can contain abstract methods, which are method declarations without a body. These methods are implicitly
- Constants:
- Interfaces can also contain constant variables, which are implicitly
public,static, andfinal. - Constant variables must be initialized at the time of declaration and cannot be modified by implementing classes.
- Interfaces can also contain constant variables, which are implicitly
- Extending Interfaces:
- An interface can extend one or more other interfaces using the
extendskeyword. - If an interface extends multiple interfaces, it inherits all abstract methods and constant variables from all the extended interfaces.
- An interface can extend one or more other interfaces using the
- 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.
- 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
defaultkeyword and can have a body. They are used to provide backward compatibility when new methods are added to existing interfaces.
- Static Methods (Introduced in Java 8):
- Java 8 also introduced static methods in interfaces, which are declared using the
statickeyword. - Static methods in interfaces provide utility methods that are associated with the interface itself, rather than with instances of implementing classes.
- Java 8 also introduced static methods in interfaces, which are declared using the
- Marker Interfaces:
- Interfaces with no methods, known as marker interfaces, are used to mark classes with certain properties.
- Examples include
Serializable,Cloneable, andRemoteinterfaces 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:
Animalis an interface containing two abstract methods (eat()andsleep()) and a constant variable (TYPE).Dogis a class that implements theAnimalinterface, providing concrete implementations for the abstract methods.- The
mainmethod demonstrates creating an object of theDogclass and calling methods defined in theAnimalinterface, as well as accessing the constant variableTYPE.
Leave a comment