cs notebook1
  • Introduction
  • sort algorithm
  • Overloading VS Overriding
  • multithreading
  • Concurrency0
  • Concurrency1
  • ExecutorService
  • iteration & recursion
  • IO
  • Marker interface(Serializable, Clonnable&Remote)
  • Jackson Library
  • java.lang.System.*
  • Virtual Memory
  • Java ClassLoader
  • interfaceVSabstractClass
  • ENUM
Powered by GitBook
On this page
  • 1. Interface:
  • 1.1 New features added in interfaces in JDK 8
  • 1.2 Important points about interface or summary of article:
  • 1.3 New features added in interfaces in JDK 9
  • 2. Abstract Classes in Java
  • 2.1 Following are some important observations about abstract classes in Java
  • 3. interface VS abstract class
  • 4. When to use what?

Was this helpful?

interfaceVSabstractClass

1. Interface:

Like a class, an Interface can have methods and variables, but the methods declared in interface are by default abstract(only method signature, no body).

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.

  • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.

  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.

  • A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.

1.1 New features added in interfaces in JDK 8

  1. Prior to JDK 8, interface could not define implementation. We can now add default implementation for interface methods. This default implementation has special use and does not affect the intention behind interfaces.

Suppose we need to add a new function in an existing interface. Obviously the old code will not work as the classes have not implemented those new functions. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work.

e.g.

//An example to show that interface can have methods from JDK 1.8 onwards
interface in1{
    final int 1 = 10;
    default void display(){
        System.out.println("hello");
    }
}

//A class that implements interface.
class testClass implements in1{
    //driver code
    public static void main(String[] args){
        testClass t = new testClass();
        t.display();
    }
}
  1. Another feature that was added in JDK 8 is that we can now define static methods in interfaces which can be called independently without an object. Note: these methods are not inherited.

//An example to show that interfaces can have methods from JDK 1.8 onwards
interface in1{
    final int a = 10;
    static void display(){
        System.out.println("hello");
    }
}
//A class that implements interface.
class testClass implements in1{
    //driver code
    public static void main(String[] args){
        in1.display();
    }
}

1.2 Important points about interface or summary of article:

  • We can't create instance (interface can't be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class.

  • A class can implement more than one interface.

  • An interface can extends another interface(but only one interface).

  • All the methods are public and abstract. And all the fields are public, static, and final.

  • It is used to achieve multiple inheritance.

  • It is used to achieve loose coupling.

1.3 New features added in interfaces in JDK 9

From Java 9 onward, interfaces can contain following also:

  1. Static methods

  2. Private methods

  3. Private Static methods

2. Abstract Classes in Java

In Java, a separate keyword abstract is used to make a class abstract. It can have abstract methods(methods without body) as well as concrete methods(regular methods with body).

2.1 Following are some important observations about abstract classes in Java

  1. Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.

  2. Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of a inherited class is created.

e.g.

//An abstract class with constructor 
abstract class Base{
    Base(){System.out.println("Base constructor called");}
    abstract void fun();
}

3. interface VS abstract class

  1. Type of methods:

    Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.

  2. Final Variables:

    Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.

  3. Type of variables:

    Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.

  4. Implementation:

    Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.

  5. Inheritance vs Abstraction:

    A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.

  6. Multiple implementation:

    An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

  7. Accessibility of Data Members:

    Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

4. When to use what?

Consider using abstract classes if any of these statements apply to your situation:

  • In java application, there are some related classes that need to share some lines of code then you can put these lines of code within abstract class and this abstract class should be extended by all these related classes.

  • You can define non-static or non-final field(s) in abstract class, so that via a method you can access and modify the state of Object to which they belong.

  • You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

Consider using interfaces if any of these statements apply to your situation:

  • It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.

  • A class can implement more than one interface. It is called multiple inheritance.

  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

PreviousJava ClassLoaderNextENUM

Last updated 6 years ago

Was this helpful?

https://www.geeksforgeeks.org/difference-between-abstract-class-and-interface-in-java