top of page

Subscribe to Wisdom

Thanks for Subscribing!

  • Writer's picturePuru Uttam

Abstract Class vs Interface in Java

Both are used to achieve abstraction in Java and can't be instantiated.

Abstraction :

It is the process of hiding the data and showing only essential information to the user.


Abstract Class :

  • abstract keyword is used to declare Abstract Class.

  • It can have abstract as well as non-abstract methods.

  • It doesn't support multiple inheritance.

  • It can extend another Java class and can implement interfaces just like any other class.

  • It can have non-final variables, final variables, static and non-static variables.

  • It can also provide implementation of the interface.

  • It can have access modifiers like private, public, protected, etc.

  • extends keyword is used to extend an abstract class.


//example of Abstract Class
public abstract class Game {
          protected abstract void start(); 
          protected abstract void stop();     
}

//Hockey is a class extending Game Abstract class so it needs to override //all the abstract methods of Game class.

public class Hockey extends Game {
@Override
protected abstract void start(){
//code implementation
}

@Override
protected abstract void stop(){
//code implementation
}
}

//as abstract class can't be instantiated.
public static void main(String... args){
Game hockey= new Hockey();
game.start();
game.stop();
}

Interface :

  • interface keyword is used to declare an Interface.

  • It can have abstract as well as default and static methods from Java 8.

  • It supports multiple inheritance.

  • It can extend another Java interfaces only.

  • It can have final variables and static variables.

  • It can't provide implementation of the abstract class.

  • It has public access modifier bydefault.

  • implements keyword is used to extend an interface by a class and extends is used when one interface extends another interface.

If we try to implement an interface with another interface, it will throw a compile-time error in Java because if interface is implemented then it's methods must be defined but interface never has the definition of any method.

//Interface extends interface
interface SecondInterface extends FirstInterface{
void c();
void d();
}


//example of an interface
interface FirstInterface{  
void a();//bydefault, public and abstract  
void b();
default void start() {
System.out.println("Default is starting...");
}
static void display() {
System.out.println("Displaying...");
}    
}  

//Class implements interface
class Hockey implements FirstInterface{
@Override
public void a(){
//code implementation
}

@Override
public void b(){
//code implementation
}
}

public static void main(String... args){
FirstInterface hockey = new Hockey();//as interface can't be instantiated
hockey.a();
hockey.b();
hockey.start();
}

Both of them should be used as per the requirement as both are very essential and play a very important role when you will proceed to learn design patterns.

17 views0 comments

Recent Posts

See All

Comments


Modern Digital Watch
bottom of page