Interface Extends Abstract Class in Java
Abstract Classes and Interfaces in Java
Java is a powerful, object-oriented programming language that has become a mainstay in software development. One of the primary features of Java is its support for abstract classes and interfaces. Abstract classes and interfaces allow developers to create more flexible and extensible classes than traditional ones. This article will discuss the differences between abstract classes and interfaces in Java, how to use them, and the advantages and disadvantages of each.
What is an Abstract Class?
An abstract class is a class that cannot be instantiated. It contains methods and fields that are usually implemented by subclasses. An abstract class provides a template for subclasses to follow but does not implement the methods. Subclasses can extend the abstract class and implement the methods, or they can implement the methods themselves. Abstract classes are typically used when a common set of functionality needs to be shared among several subclasses.
What is an Interface?
An interface in Java describes a set of methods and variables that must be defined by a class that implements the interface. The interface contains no implementation code, only method and variable declarations. Classes that implement an interface must provide implementations for all the methods and variables declared in the interface. Interfaces are typically used when a common set of functionality needs to be shared across several unrelated classes.
Difference between Abstract Classes and Interfaces
The primary difference between abstract classes and interfaces is that abstract classes can contain implementation code, while interfaces cannot. Abstract classes are typically used when a common set of functionality needs to be shared among several subclasses. In contrast, interfaces are typically used when a common set of functionality needs to be shared across several unrelated classes.
Another difference between abstract classes and interfaces is that abstract classes can contain fields, while interfaces cannot. Abstract classes can also have constructors, while interfaces cannot.
Program
InterfaceAbstract.java
//An example of a Java program which implements an interface
//and extends an abstract class
public class InterfaceAbstract extends AbstractClass implements Interface {
public void method1() {
System.out.println("This is method 1 of InterfaceAbstract class");
}
public void method2() {
System.out.println("This is method 2 of InterfaceAbstract class");
}
public void method3() {
System.out.println("This is method 3 of InterfaceAbstract class");
}
public static void main(String[] args) {
InterfaceAbstract obj = new InterfaceAbstract();
obj.method1();
obj.method2();
obj.method3();
obj.abstractMethod();
}
}
abstract class AbstractClass {
public void abstractMethod() {
System.out.println("This is abstract method of AbstractClass");
}
}
interface Interface {
public void method1();
public void method2();
public void method3();
}
Output
This is method 1 of InterfaceAbstract class
This is method 2 of InterfaceAbstract class
This is method 3 of InterfaceAbstract class
This is an abstract method of AbstractClass
Using Abstract Classes and Interfaces
When creating a class in Java, you can use either an abstract class or an interface. Which one you choose depends on the requirements of your project and the functionality you need to implement.
An abstract class is the best option if you need to share functionality among several subclasses. An abstract class can contain fields and implementation code, so it is the best choice for sharing functionality among subclasses.
An interface is the best option if you need to share functionality across several unrelated classes. An interface cannot contain fields or implementation code, so it is the best choice for sharing functionality across unrelated classes.
Advantages of Abstract Classes
The primary advantage of an abstract class is that it can contain fields and implementation code. This allows the abstract class to provide a template for subclasses to follow while providing some functionality. This makes abstract classes more flexible and extensible than traditional classes.
Another advantage of abstract classes is that they can be used as a base class for multiple subclasses. This allows a single abstract class to be used as the foundation for multiple subclasses, which can help reduce the amount of code that needs to be written.
Disadvantages of Abstract Classes
The primary disadvantage of an abstract class is that it cannot be instantiated. This means an abstract class cannot be used directly, only through a subclass. This can make it difficult to use an abstract class in certain situations.
Another disadvantage of abstract classes is that they can become overly complex. An abstract class must contain more implementation code to make it easier to understand and maintain.
Advantages of Interfaces
The primary advantage of an interface is that it can be used to share functionality across several unrelated classes. This makes it easier to share functionality between classes without having to duplicate code.
Another advantage of interfaces is that they can be used to create “plug and play” classes. This means classes that implement an interface can be used interchangeably without changing code.
Disadvantages of Interfaces
The primary disadvantage of an interface is that it cannot contain fields or implementation code. This means an interface cannot provide functionality, only method and variable declarations. This can make it difficult to use an interface in certain situations.
Another disadvantage of interfaces is that they can be difficult to maintain, as any changes to the interface must be propagated to all of the classes that implement the interface.
Conclusion
Abstract classes and interfaces are two of the most important features of Java. Abstract classes are typically used when a common set of functionality needs to be shared among several subclasses. In contrast, interfaces are typically used when a common set of functionality needs to be shared across several unrelated classes. Each has its advantages and disadvantages, and it is important to understand their differences to choose the best option for your project.