Java Abstract and Interface

In this article we will look into important topic Abstraction. Abstraction is one of the core concepts of OOP. Abstraction allows us to hide the implementation details provide simpler, high-level interface for interacting with system. We can achieve abstraction by using Abstract class or Interface.

Java Abstract and Interface
Java Abstract and Interface

Introduction

Abstract keyword is a non-access modifier in Java that is used to declare classes and method.
Abstract Class is defined as a class with “abstract” keyword. We cannot create an object of Abstract class directly.
An “abstract means something incomplete and to complete the class or method, we must provide the implementation by subclass” as similar to interface in java.

How to achieve or implement Abstraction in Java?
Abstraction can be achieved in Java through the use of abstract classes and interfaces.

  • An abstract class is a class that cannot be instantiated directly and must be extended by a subclass.
  • An interface is a collection of abstract methods that must be implemented by any class that implements the interface.

Advantage of Abstraction

  • Improved code reuse
  • Increased modularity
  • Improved code maintainability

The disadvantages of using abstraction

  • Increased overhead and complexity

Abstract class in Java?
To create an abstract class in Java, we use the “abstract” keyword in the class declaration, like

public abstract class Example {
 // abstract methods and other members here
    abstract type MethodName(arguments); // No body
}

Any class that extends an abstract class must implement any abstract methods that are defined in the abstract class.
What is Abstract method in Java.
Method that doesn’t contain any body, simply the signature of method declaration.

For example:
abstract void action(); // No body.

An abstract method can be used when:

  • When the same method has to perform different tasks depending on the object calling it.
  • When you need to be overridden in its non-abstract subclasses.

Some Important point in respect to Abstract Method.

Why we can’t declare abstract method be as static.
We cannot declare abstract with static keyword in java, as static method and abstract method serve two different purpose, it not possible to declare an abstract method as static because.

1. Static method belong to class rather than to any particular instance in java
. it’s me that, we can call static method without creating object of class, just by using class name.
2. Abstract method is something that is incomplete, we must provide implementations.

Private modified with abstract method
No, We can’t.  if we declare it as private, we cannot provide its implementation in subclass.

Constructor and abstract class.
Yes, an abstract class can have a constructor, but we cannot use it directly.
it’s called from the subclass constructor to initialize the attribute inherited form the superclass.

Example
public abstract class Point {
  protected int x;
  protected int y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
   public abstract void draw();
}

Final and Abstract keyword.
final” keyword with method “it means that the method cannot be overridden by any subclass”.
abstract” keyword with method means “we must provide implementation in subclass for further use”.

Java Abstract classes are similar to interfaces in some ways

Both cannot instantiate directly.
Both contain method declared and define with or without body.

When to Use an Abstract
If we have specified requirements and only partial implementation details
If we want the class to have code other than just abstract methods
If subclass inherits common behavior but can override it in case the subclass needs a specific implementation of common behavior.

When to Use an Interface
If we want loose coupling.
if we want full abstraction.
If we need to define a contract, without bothering about implementation.
if we want to support dynamic resolution at runtime

Till Java 7 all method in an interface are implicitly abstracts since Java 8 onward lots of changes are done in Interface.

Note:

  • A class will become abstract class, when any of its methods declared as abstract.
  • If a class extends an abstract class or interface, it must provide implementation to all its abstract methods in subclass. otherwise, subclass also become abstract class.

8 thoughts on “Java Abstract and Interface”

Leave a Comment