Abstract Class And Interface In Java

Abstraction is a process of hiding the internal details of an application from the world and showing only functionality to the user.

Example – A man driving a car. The man only knows that applying brakes will stop the car, but he does not know about the inner mechanism of the car or the implementation of brakes that how it is working in the car. This is called abstraction.

In Java, abstraction is achieved by interfaces and abstract classes. We can achieve abstraction using interfaces and using abstract classes. With abstract classes, we can achieve abstraction 0-100% and by interfaces, we can achieve abstraction 100%.

Abstract Classes-

Abstract keyword is used to declare the abstract class in Java.  It can have both abstract method and non-abstract methods (concrete method) also.

The implementation of the abstract method is provided in the subclass. It cannot be instantiated means we can not create the object of the abstract classes.

  • A class that is declared with an abstract keyword is known as an abstract class.
  • It can have both abstract and non-abstract (concrete) methods.
  • It cannot be instantiated.
  • It can have constructors (because we use a constructor when the variable is not final and static to initialize it) and static methods.
  • It can have final methods also.

 

Syntax of abstract class-

abstract class A{}

 

Abstract Method in Java-

A method that is declared with an abstract keyword and does not have implementation or has its method body in a subclass is known as an abstract method.

Example-

abstract void printStatus();  //no method body and abstract

 

Example-

In the below example, Bike is an abstract class that contains only one method run which is an abstract method. And Its implementation is provided by the Honda class which is the subclass of the class bike because it is extending the bike class.

abstract class Bike{  

  abstract void run();  



class Honda extends Bike{  

void run(){

  System.out.println("running safely");

 }  

public static void main(String args[]){  

 Bike obj = new Honda4();  

 obj.run();  



Output-

running safely

 

An abstract class having a constructor, data member, and methods-

An abstract class can have a data member, abstract method, non-abstract method, constructor, and even main() method.

Example-

 abstract class Bike{  

   Bike(){

    System.out.println("bike is created");

    }  

   abstract void run();  //abstract method

   void changeGear(){

    System.out.println("gear changed");

   }  

 }  

//Creating a Child class which inherits Abstract class(Bike class) 

 class Honda extends Bike{  

 void run(){

   System.out.println("running safely..");

  }  

 }  

 class TestAbstraction2{  

 public static void main(String args[]){  

  Bike obj = new Honda();  

  obj.run();  

  obj.changeGear();  

 }  

Output-

The bike is created

To run safely.

Gear changed

 

Interface in Java-

In Java, an interface is a reference type that is similar to a class. The interface specifies what methods a class should implement. A Java interface can contain static, abstract, default, and private methods since Java 8.

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, nobody) and variables are by default public, static, and final.

 

  • It also can not be instantiated just like the abstract class.
  • All the methods are public and abstract by default in an interface without declaration.
  • All the interface fields are public, static, and final by default.
  • Since Java 8, we can have default methods and static methods in an interface.
  • Since Java 9, we can have private methods also in an interface.
  • The interface does not have a constructor but the abstract class can have one.

 

Use of Java interface-

  • The interface is used to achieve abstraction (100%).
  • We can support the functionality of multiple inheritances by the interface.
  • It is used to achieve loose coupling.

 

Declaration of an interface-

An interface is declared by using the interface keyword and then the name of the interface. A class that implements an interface must implement all the methods which is declared in the interface.

Syntax:

interface <interface_name> {

    // declare constant fields

    // declare methods that abstract

    // by default.  

}

 

Example of Interface-

In this example, the Drawable interface has only one method and its implementation is provided by Rectangle and Circle classes. The implementation part (method body) is hidden by the user who uses the interface.

interface Drawable{  

void draw();  



//Implementation: by second user 

class Rectangle implements Drawable{  

public void draw(){

  System.out.println("drawing rectangle");

 }  



class Circle implements Drawable{  

public void draw(){

  System.out.println("drawing circle");

 }  



//Using interface: by third user 

class TestInterface1{  

public static void main(String args[]){  

Drawable d=new Circle();

d.draw();  

}

 

Output-

Drawing circle

 

Multiple inheritances in Java by interface-

If a class implements multiple interfaces, or interface extends multiple interfaces, it is known as multiple inheritances.

A class can extend only one class but can implement the interface once or more than once.

Example-

In the below example, there are Printable and Showable interfaces and class A7 is implementing a Printable and Showable interface and the implementation of the methods of interfaces are provided in the class which is implementing the interfaces.

interface Printable{  

void print();  



interface Showable{  

void show();  



class A7 implements Printable,Showable{  

public void print(){

 System.out.println("Hello");



public void show(){

 System.out.println("Welcome");



public static void main(String args[]){  

A7 obj = new A7();  

obj.print();  

obj.show();  



}

 

Output

Hello

Welcome

 

Java 8 Default and Static Method in Interface-

interface Drawable{  

void draw();  

default void msg(){

 System.out.println("default method");

} 

static int cube(int x){ 

  return x*x*x;

 }  



class Rectangle implements Drawable{  

 public void draw(){

  System.out.println("drawing rectangle");

 }  



class TestInterfaceDefault{  

public static void main(String args[]){  

Drawable d=new Rectangle();  

d.draw();  

d.msg();  

System.out.println(Drawable.cube(3));  

}



Output-

 

Drawing rectangle

Default method

27

 

Difference between abstract class and interface-

 

        Abstract Class         Interface
1. Abstract class can have abstract and non-abstract methods(concrete method). 1. Interface can have only abstract methods.

Since Java 8, it can have default and static methods also and since Java 9, it can have private methods also.

2. It does not support multiple inheritances. 2. It supports multiple inheritances.
3. It can have final, non-final, static, and non-static variables. 3. It has only static and final variables.
4. In this abstract keyword is used to declare an abstract class. 4. The interface keyword is used to declare the interface.
5. An abstract class can extend another Java class and can implement multiple Java interfaces. 5. An interface can only extend another Java interface.
6. An abstract class can be extended using the “extends” keyword. 6. For the interface we use the “implements” keyword for implementation.
7. An abstract class can have class members like private, protected, etc. 7. There is a restriction on members because members of a Java interface are public, static, and final by default.
Example:

public abstract class Shape{

public abstract void draw();

}

Example:

public interface Drawable{

void draw();

}

 

Leave a Reply