• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Difference » Difference Between Interface vs Abstract Class In Java

Difference Between Interface vs Abstract Class In Java

Last Updated on: October 20, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Interface vs Abstract Class In Java
  • Abstract class vs. The interface in Java 8
  • Example of Interface and Abstract class
  • Interface:
  • Abstract Class:
  • Abstract Method:

Difference Between Interface vs Abstract Class In Java: In this article, we will list out the difference between an interface and an abstract class in Java. Let us detail out the difference between interface v/s abstract class in tabular form below,

Read Also: Method vs. Constructor In Java

Interface vs Abstract Class In Java

Interface Abstract class
All methods inside an interface are implicitly abstract interface helps to achieve 100% abstraction An abstract class can contain both abstract methods and concrete method commonly used in factory design pattern
A constructor is not allowed Interface An abstract class can have constructor; both default and parameterized constructor allowed
An interface can’t be instantiated Abstract classes too can’t be instantiated even though having constructor with instance data members
Implicitly, all methods declared inside an interface are public An abstract class can have abstract methods with protected or public access-modifier
An interface can extend any number of other interfaces An abstract class can extend only one class; it could be either concrete or another abstract class
An interface is a mean by which Java supports multiple inheritances An abstract class doesn’t support inheritance
an abstract keyword for methods inside an interface is optional an abstract keyword is a must for abstract methods inside an abstract class
Variables defined inside an interface are implicitly public, static and final, i.e., CONSTANT Variables can be static or final or both with any access modifier
No such things are allowed in abstract classes An abstract class can have a static main() method to execute the class
Only abstract methods allowed Static methods are allowed inside an abstract class

Abstract class vs. The interface in Java 8

At a very high level, it looks very similar, but actually, they are different in many ways. Also, because the default method in interface helps us to achieve loose coupling and backward compatibility

Abstract Classes Interface
Contains members variables All variables are constants, i.e., public, static and final
It can have constructors An interface cannot have constructors
Can hold the state of an object using instance member variables Since all variables are static and final therefore no concept of holding the state of an object
Forces to implement abstract methods or else declare the class as abstract default methods can be overridden, if required but never forces
Concrete methods are allowed; in addition to abstract methods Concrete methods should be either default or static; otherwise, only abstract methods are allowed

Example of Interface and Abstract class

1) Example on Interface:

Variable are implicitly public, static and final; and methods are implicitly public and abstract

Java7Interface.java

package in.bench.resources.itf.example;
public interface Java7Interface {
    // variables are implicitly public, static and final
    String ORGANIZATION_NAME = "ABC Pvt. Ltd.";
    // methods are implicitly public and abstract
    void display();
}

2) Example on Interface in Java 8

Variable are implicitly public, static, and final, and methods are implicitly public and abstract for methods without implementation. Other than abstract public methods; default and static method with implementation are allowed inside Java 8 interface

InterfaceInJava8.java

package in.bench.resources.itf.example;
 
// only public abstract are permitted
public interface InterfaceInJava8 {
 
    // old abstract method
    void displayAbstractMethod(); // by default, this is public and abstract
 
    // default method with concrete implementation from Java 8
    default void displayDefaultMethod() {
        System.out.println("InterfaceInJava8 : default method impl inside interface");
    }
 
    // static method with concrete implementation from Java 8
    static void displayStaticMethod() {
        System.out.println("InterfaceInJava8 : static method impl inside Java Interface");
    }
 
}

3) Example of an Abstract class and an abstract method

Variables can be both instance and static data members; methods can be abstract or concrete or static. Though constructor is allowed, instantiation of abstract classes are not allowed but can have the main() method and execute as well

AbstractExample.java

package in.bench.resources.abstractclass.example;
 
// abstract class
public abstract class AbstractExample extends DemoAbstract {
 
    String demoString;
    static int demoCounter = 1;
 
    // default no-arg constructor
    AbstractExample(){
        // do some initialization logic here
    }
 
    static void staticMethod() {
        System.out.println("AbstractExample: static methods are allowed inside abstract class");
    }
 
    // abstract method declaration in abstract class
    abstract void myAbstractMethod();
 
    // concrete method definition in abstract class
    void myConcreteMethod() {
        System.out.println("AbstractExample: This is my concrete method in abstract class");
    }
 
    // main() method - the entry point to JVM to begin execution
    public static void main(String args[]) {
 
        System.out.println("Accessing static field demoCounter : " + AbstractExample.demoCounter);
        staticMethod();
    }
}

Points to remember about Interface and Abstract classes & methods:

Read Also: Common Manual Interview Questions

Interface:

  • An interface is 100% pure abstraction, but post-Java 8 release, default and static methods are allowed
  • An interface is a mean by which Java supports multiple inheritances
  • The constructor is not allowed inside interface
  • Instantiation of an interface is not allowed
  • All variables are implicitly public, static and final
  • Apart from default and static methods, the method with no implementation are implicitly public and abstract (old abstract method)
  • An interface can be used as a reference variable for sub-class instantiation

Abstract Class:

  • An abstract class is declared with an abstract keyword in a class declaration
  • Extending or sub-classing abstract class must provide implementation details to all abstract methods
  • Else make extending class as abstract which means next implementing class must provide concrete implementation for all abstract methods
  • An abstract class can contain a mix of both, i.e., abstract methods & concrete methods
  • It can have both default & parameterized constructor, but still, it cannot be instantiated
  • An abstract class with no abstract method is denoted that this class cannot instantiate, rather we can create an object of the extending a class (subclass)
  • An abstract method is not allowed inside a concrete class and compiler throws an error stating “requires method body instead of a semicolon(;)”
  • Both static and non-static fields can be declared inside an abstract class
  • An abstract class can extend only one class, and it could be either an abstract class or concrete class
  • An abstract can have a main() method – the entry point to JVM to begin execution

Abstract Method:

  • An abstract method has no-body.
  • Method signature ends with a semicolon(;)
  • An abstract method can throw an exception
  • Abstract methods can be declared inside the abstract class only
  • Abstract methods cannot be final or private or static
  • Only protected, default and public access modifiers allowed

    Filed Under: Difference

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy