Difference between Abstract Class and Interface

HOME

Abstract Class and Interface are used to achieve Abstraction where abstract methods are declared. We can’t instantiate Abstract Class and Interface.

Abstract ClassInterface
Abstract Class can have both abstract method and
non-abstract methods.
Interface can have only abstract methods.
Abstract Class does not support multiple Inheritance.Interface supports multiple Inheritance.
Abstract Class can be implemented by using keyword “extends”. Interface can be implemented by using keyword “implements”.
Abstract class can provide the implementation of interface.Interface can’t provide the implementation of abstract class.
Abstract Class can have static, non-static, final &
non-final variables.
Interface can have only final & static variables.
Abstract Class can have class members as private, protected, etc.Interface can have class members as public by default.
Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
An abstract class can extend another Java class and
implement multiple Java interfaces.
An interface can extend another Java interface only.
Abstract keyword is used to declared abstract classInterface keyword is used to declare interface.

Below is an example of Abstract Class. In the below example, there is an abstract class with abstract method. This abstract method is implemented in SubClass.

//Abstract Class
public abstract class Bank {
 
    // Abstract method
    abstract void getRateOfInterest();
}

SubClass

//Subclass (inherit from Bank)
public class SBI extends Bank {
 
    // The body of getRateOfInterest is declared here
    public void getRateOfInterest() {
 
        System.out.println("Interest Rate of SBI Bank :" + 5.3);
 
    }
}


Test Class

public class AbstractionTest {
 
    public static void main(String[] args) {
 
        Bank bank1 = new SBI();
        bank1.getRateOfInterest();
 
    }
 
}

Below is an example of Interface. In the below example, Interface has two abstract methods and the implementation of abstract methods are declared in Test Class.

public interface MyInterface {
 
    // Abstract methods
    public void method1();
 
    public void method2();
}

Test Class

public class InterfaceTest implements MyInterface {
 
    public void method1() {
        System.out.println("Implementation of Method 1");
    }
 
    public void method2() {
        System.out.println("Implementation of Method 2");
    }
 
    public static void main(String[] args) {
        MyInterface interTest = new InterfaceTest();
        interTest.method1();
        interTest.method2();
 
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s