Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users.

In java, we can do the abstraction. in simple words , abstraction means – something happened but everyone does not know. Let’s say, is we switch on fan , then fan start on, but we don’t need to know the electricity wiring of the fan.

We can implement abstraction using either ‘abstract class’ or ‘interfaces’.

Abstract class:

  • Properties of abstract class ->
  • An instance of an abstract class can not be created.
  • Constructors are allowed.
  • We can have an abstract class without any abstract method.
  • There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final  or in simpler terms final method can not be abstract itself as it will yield an error: “Illegal combination of modifiers: abstract and final”.

Let’s understand below, usage of abstract class and abstract method …

abstract class A{
    abstract void car(String m);
}
class TATA extends A{
    void car(String a){   
    System.out.println(“car model is “+a);
    }
}
public class file { 
      public static void main(String[] args) {   
    TATA t1 = new TATA();   
    t1.car(“jaguar”); 
  }
}
output

Note: An abstract class can contain constructors in Java. And a constructor of an abstract class is called when an instance of an inherited class is created. Let’s understand below code…

// Abstract class
abstract class Base {
    // Constructor of class 1
               Base()
               {
                              // Print statement
                              System.out.println(“Base Constructor Called”);
               }
 
               // Abstract method inside class1
               abstract void fun();
}
// child class
class Derived extends Base {
    // Constructor of class2
               Derived()
               {
                              System.out.println(“Derived Constructor Called”);
               }
    // Method of class2
               void fun()
               {
                              System.out.println(“Derived fun() called”);
               }
}
// Main class
class abstract1 {
    // Main driver method
               public static void main(String args[])
               {
                              // Creating object of class 2
                              // inside main() method
                              Derived d = new Derived();
                              d.fun();
               }
}
output

Interfaces

Before starting the concept of interfaces let’s see why we need to use this.

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
  • It is also used to achieve loose coupling.

Let’s understand the concept of interfaces through below code…

interface Polygon {
  void getArea(int length, int breadth);
}
 
// implement the Polygon interface
class Rectangle implements Polygon {
 
  // implementation of abstract method
  public void getArea(int length, int breadth) {
    System.out.println(“The area of the rectangle is ” + (length * breadth));
  }
}
 
class Main {
  public static void main(String[] args) {
    Rectangle r1 = new Rectangle();
    r1.getArea(2, 7);
  }
}
output
interface FirstInterface {
  public void myMethod(); // interface method
}
 
interface SecondInterface {
  public void myOtherMethod(); // interface method
}
 
// DemoClass “implements” FirstInterface and SecondInterface
class DemoClass implements FirstInterface, SecondInterface {
  public void myMethod() {
    System.out.println(“Some text..”);
  }
  public void myOtherMethod() {
    System.out.println(“Some other text…”);
  }
}
 
class interFaces{
  public static void main(String[] args) {
    DemoClass myObj = new DemoClass();
    myObj.myMethod();
    myObj.myOtherMethod();
  }
}
output

Ok!! Now let’s understand the basic differences between interfaces and abstract class.

Above table source: https://www.simplilearn.com/abstract-class-vs-interface-java-article