Abstraction and Interfaces in Java

Introduction:

As we know that abstraction means only displaying functionality to users while hiding the technical implementation of the feature. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction, so that Interface and Abstract Class are required prerequisites.




Abstraction reduces the programming efforts and the complexity. An end-user using the application need not be concerned about how a particular feature is implemented. He/she can just use the features as required.

As a result, in abstraction, we work with ideas rather than actual events. This means that we only reveal the functionality to the user and hide the implementation specifics from them. The user will only be aware of "what it does" as a result and not "how it does."

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

An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.

Abstract classInterface
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?

Java allows for the usage of abstract classes and interfaces to achieve data abstraction. Despite the fact that both are methods for attaining data abstraction, they are very dissimilar from one another. 
The table above illustrates the differences between a Java abstract class and an interface. An interface can achieve total abstraction, whereas an abstract class can achieve partial or complete abstraction. This is the main distinction between an abstract class and an interface. If there are several implementations of a same method signature, it is advisable to use an interface. On the other hand, when several implementations of the same sort share a common behaviour, an abstract class is a great option.

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. James Hartman, "Interface vs Abstract Class in Java: What’s the Difference?",(2022).
3. Rafael del NeroJava Developer, InfoWorld ,"Abstract classes vs. interfaces in Java," (2022).
4. https://www.javatpoint.com/difference-between-abstract-class-and-interface

Author

Shivani Padamwar



Comments

Post a Comment