Abstraction and Interfaces in Java
Introduction:
What Is Abstract Class?
A class which has the abstract keyword in its declaration is called abstract class. At least one abstract method must be present in abstract classes. i.e., methods without a body. It can have multiple concrete methods.
Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.
Abstract classes cannot be instantiated.
What is Interface?
The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.
An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces may have public static final variables, but they never contain instance variables (i.e., constant class variables).
Important Reasons For Using Abstract Class
- Abstract classes offer default functionality for the subclasses.
- Provides a template for future specific classes
- Helps you to define a common interface for its subclasses
- Abstract class allows code reusability.
Important Reasons For Using Interfaces
- Interfaces are used to achieve abstraction.
- Designed to support dynamic method resolution at run time
- It helps you to achieve loose coupling.
- Allows you to separate the definition of a method from the inheritance hierarchy
Interface Vs. Abstract Class
| Abstract class | Interface |
|---|---|
| 1) Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. Since Java 8, it can have default and static methods also. |
| 2) Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
| 3) Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
| 4) Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
| 5) The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
| 6) An abstract class can extend another Java class and implement multiple Java interfaces. | An interface can extend another Java interface only. |
| 7) An abstract class can be extended using keyword "extends". | An interface can be implemented using keyword "implements". |
| 8) A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
| 9)Example: public abstract class Shape{ public abstract void draw(); } | Example: public interface Drawable{ void draw(); } |
Interface vs Abstract Class - Which One to Choose?
Interface Syntax
interface name{
//methods
}Java Interface Example:
interface Pet {
public void test();
}
class Dog implements Pet {
public void test() {
System.out.println("Interface Method Implemented");
}
public static void main(String args[]) {
Pet p = new Dog();
p.test();
}
}Abstract Class Syntax
abstract class name{
// code
}Abstract Class Example:
abstract class Shape {
int b = 20;
abstract public void calculateArea();
}
public class Rectangle extends Shape {
public static void main(String args[]) {
Rectangle obj = new Rectangle();
obj.b = 200;
obj.calculateArea();
}
public void calculateArea() {
System.out.println("Area is " + (b * b));
}
}Conclusion
The most important feature of object-oriented programming is abstraction, and Java offers two methods to do it: an abstract class and an interface. An abstract class can contain both abstract and non-abstract methods, whereas an interface is a collection of abstract methods. As a result, abstract classes can offer both partial and complete abstraction, whereas interfaces can only offer complete abstraction.
References
1. https://www.geeksforgeeks.org/difference-between-abstract-class-and-interface-in-java/2. Interface vs Abstract Class in Java: What’s the Difference?",(2022).3. Rafael del Nero, Java Developer, InfoWorld ,"Abstract classes vs. interfaces in Java," (2022).4. https://www.javatpoint.com/difference-between-abstract-class-and-interfaceAuthor
Shivani Padamwar
Informative!!!!
ReplyDeleteGreat work👍
ReplyDeleteNiceee
ReplyDeleteVery Interesting 🙌🙌
ReplyDeleteInformative blog🤝
ReplyDeleteQuality information
ReplyDelete