Online Tutorials & Training Materials | STechies.com
Register Login

Difference between Abstract Class and Interface

|| 0

Difference between Abstract Class and Interface
Stechies

Before we begin with the differences between an abstract class and interface in java, it is important to understand the meaning of the term abstraction. Abstraction pertains to concealing the internal implementation specific features of objects and showcasing just the functionality to users. Abstract class and interface are both used for the purpose of abstraction. An important and frequently-asked interview question, the difference between interface and abstract class forms a core component of java programming language. So, as a Java programmer, if you have been wondering whether to choose abstract class or interface, then what follows is for you.

Interface vs Abstract Class

The differences between abstract class and interface are as follows:

Abstract Class

Interface

At any given point of time, an abstract class is capable of extending only one class or a singular abstract class.

Aninterface is designed to extend multiple interfaces at any given point of time.

Abstract classes are capable of extending other concrete (regular) classes and/ or abstract classes.

Interfaces are capable of extending other interfaces only.

Anabstractclasscomprises of both concrete andabstract methods.

Only abstract methods are present in an interface.

The keyword “abstract” is used for declaring any method as an abstract method. It is mandatory.

The keyword “abstract” is not mandatory for the purpose of declaring abstract methods.

An abstract class may comprise of both protected and public abstract methods.

Interfaces in java comprise of public abstract methods only.

Abstract classes are capable of having final, static, final and/or static finalvariables with the use of access specifiers.

Only public static final variables are possible with interfaces in java.

The ‘abstract’ keyword is used for defining an abstract class and method alike.

The ‘interface’ keyword is used for defining an interface only. It cannot be used for method declaration.

The ‘extends’ keyword is used by a subclass for extending an abstract class. Implementation of all declared methods present in the abstract class have to be provided in case the subclass is not an abstract class.

The ‘implements’ keyword is used by a subclass for the implementation of an interface. All the methods present in the interface have to be provided with proper implementation for the code to compile.

Abstract classes may have methods with implementation.

Java interfaces offer absolute abstraction. It is not possible to have method implementations in them.

Abstract classes possess constructors.

Interfaces do not possess constructors.

Abstract class contain the features of any normal java class but these features cannot be instantiated.

Interfaces comprise of a different type and comprise of public static final method declarations and constants.

Abstract class can be made to run if they contain themain()method.

An interface cannot be made to run as it does not contain the main method implementation.

An abstract class also defines contract along with providing other method implementations for usage by subclasses.

Interface in java is used for defining the contract for subclasses.

Abstract classes may comprise of non-finalvariables.

Variables declared in any Java interfaceare by defaultfinal.

Abstract classes are faster than interfaces in Java.

Java interfaces are slower than abstract classes as they require extra indirection.

Abstract class in Java

Abstract class in Java serves to be similar to interface with the exception being that it can be used for containing default method implementation.

  • Abstract classes may contain the abstract method without having a body; also, they can contain methods along with implementation.
  • The ‘abstract’ keyword is used for the creation of an abstract class and/or method.
  • It is not possible to initiate an abstract class in Java.
  • The abstract class is generally used for providing a base for a subclasses to implement and extend and implement abstract methods. It is also used to override/use the implemented methods in any given abstract class.
  • If a class contains abstract methods, then it has to be defined as abstract by using the ‘abstract’ keyword; otherwise, it will fail to compile.
  • It is optional to have an abstract class for a class to contain the abstract method. A class is capable of being marked as abstract even if it fails to declare abstract methods.
  • As Java is non-supportive of multiple class inheritance, it is preferable to utilize interface in case there is no method implementation in the abstract class.
  • In Java codes, the subclass of any abstract class has to implement all abstract methods unless the specific subclass also happens to be an abstract class.
  • Abstract classes in Java are capable of being run like all other classes if they possess the main() method.
  • Java Abstract class provides common method implementations for subclasses so as to provide default implementation.
  • Java abstract classes can implement interfaces even without the provision/implementation of interface methods.

Java Abstract Class Program Example

For Beginners

//Simple program for Abstract Class

//Abstract Class of the program
abstract class Employee {
    public void get(){
        System.out.println("I am a employee");
    }
}

//Admin Class extends the features of Employee Class
class Admin extends Employee {
    //Admin Class Method
}

//Admin Engineers extends the features of Employee Class
class Engineers extends Employee {
    //Engineers class method
}

//Main Class of the program
public class Main{
    //Main method of the program
    public static void main(String []args) {
        //Creating the an object for Admin Class
        Admin a1 = new Admin();
        //Creating the an object for Engineer Class
        Engineers e1 = new Engineers();
        //Calling Abstract Class method using Admin Class object
        a1.get();
        //Calling Abstract Class method using Engineers Class object
        e1.get();
    }
} 

OUTPUT

I am a employee                                                                                                                
I am a employee

For Experienced

//Example for abstract class in java

//importing the Scanner class of util package
import java.util.Scanner;

//Abstract Class of the program
abstract class BankName{
    //Abstract method of the Abstract Class
    abstract int getRateofInterest();
}

//BOI CLASS extends the abstract class of the program
class BOI extends BankName{
    //Implementation of the abstract method of the abstract method
    int getRateofInterest(){
        //returning the rate of interest
        return 6;
    }
}

//BOI CLASS extends the abstract class of the program
class PNB extends BankName{
    //Implementation of the abstract method of the abstract method
    int getRateofInterest(){
        //returning the rate of interest
        return 7;
    }
}
//kodek CLASS extends the abstract class of the program
class kodek extends BankName{
    //Implementation of the abstract method of the abstract method
    int getRateofInterest(){
        //returning the rate of interest
        return 8;
    }
}
//Main Class of the program
public class Main{
    //Main Method of the program
    public static void main (String[] args) {
        //Creating a object for Scanner Class
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Principal Amount : ");
        //Taking Principal Amount value from user
        float principalAmount = input.nextFloat();
        System.out.println("Enter the time period (in years) : ");
        //Taking Time Period value from user
        float timePeriod = input.nextFloat();

        BankName x;
        //Creating the object for BOI Class to access it's components
        x = new BOI();
        //Calculating the Simple Interest and Storing value in variable
        float SimpleInterestfromBOI = ( principalAmount * timePeriod * x.getRateofInterest()) / 100;
        //Printing Output for BOI CLASS
        System.out.println("Interest form BOI is : "+SimpleInterestfromBOI);

        //Creating the object for PNB Class to access it's components
        x = new PNB();
        //Calculating the Simple Interest and Storing value in variable
        float SimpleInterestfromPNB = ( principalAmount * timePeriod * x.getRateofInterest()) / 100;
        //Printing Output for PNB CLASS
        System.out.println("Interest form PNB is : "+SimpleInterestfromPNB);

        //Creating the object for kodek Class to access it's components
        x = new kodek();
        //Calculating the Simple Interest and Storing value in variable
        float SimpleInterestfromKodek = ( principalAmount * timePeriod * x.getRateofInterest()) / 100;
        //Printing Output for KODEK CLASS
        System.out.println("Interest form kodek is : "+SimpleInterestfromKodek);
    }
} 

OUTPUT 

Enter the Principal Amount :
25000
Enter the time period (in years) :
7
Interest form BOI is : 10500.0
Interest form PNB is : 12250.0
Interest form kodek is : 14000.0 

Interface in java

Interface is a reference type, which is similar to a class in Java. It contains a group of abstract methods. An interface and the abstract methods contained by the interface are inherited by Java classes. In addition to the abstract methods, java interface may also comprise of default methods, constants, static methods as well as nested types.

  • The method bodies are present only for static methods and default methods.
  • The coding of an interface is akin to the writing of java classes. However, while a class depicts the behaviour and attributes of objects, the interface comprises of the behaviour implemented by a class.
  • In case the class implementing the interface is not abstract, the methods contained by the interface have to be defined in the class.
  • The ‘interface’ keyword is used for the declaration of an interface. 
  • An interface is abstract by default. Therefore, the abstract keyword need not be used while declaring an interface.
  • All methods contained in an interface are implicitly abstract, thus it is not necessary to use the abstract keyword.
  • Methods contained in java interface are implicitly public.
  • An interface can be made to extend in another interface in a manner similar to a class that is extendible into another class. To extend an interface, programmers in java use the ‘extends’ keyword. Once the interface is extended the child interface will inherit the methods depicted by the parent interface.

Java Interface Class Program Example

//Java program for Interface

//Creating an interface for Addition
interface arithmeticAdd{
    //By default it is looks like public abstract void add ()
    void add();
}

//Creating an interface for Multiplication
interface arithmeticMul{
    //By default it is looks like public abstract void multiply ()
    void multiply();
}

//Creating a class and which inherit the all methods of arithmeticAdd Interface
class addOperation implements arithmeticAdd {
    //implementing the add function of the arithmeticAdd Interface
    public void add(){
        int x = 50;
        int y = 60;
        int z = x+y;
        //Printing the output
        System.out.println("Addition is : "+z);
    }
}

//Creating a class and which inherit the all methods of arithmeticMul Interface
class mulOperation implements arithmeticMul {
    //implementing the add function of the arithmeticMul Interface
    public void multiply(){
        int x = 50;
        int y = 60;
        int z = x*y;
        //Printing the output
        System.out.println("Multiplication is : "+z);
    }
}

//Main Class of the program
public class Main{
    //Main method of the program
    public static void main(String[] args){
        //Creating the object of addOperation Class
        arithmeticAdd addObject = new addOperation();
        //Accessing the function of addOperation Class
        addObject.add();

        //Creating the object of addOperation Class
        arithmeticMul mulObject = new mulOperation();
        //Accessing the function of mulOperation Class
        mulObject.multiply();
    }
}

Output:

Addition is : 110
Multiplication is : 3000