Menu
Topics Index
...
`


Interfaces, Packages and Access Control >
Siva Nookala - 15 Mar 2016
There are some differences and similarities between Interfaces and Abstract Classes. They are explained below.

Differences:
Interfaces Abstract Classes
Interfaces can contain only abstract methods. We can not define any concrete methods in an interface. Abstract classes can contain both abstract methods and concrete methods.
Interfaces can not have any member variables. But they can have static variables. Abstract classes can contain member variables.
All methods in an interface are by default public. We can not change the access specifier to private or protected. Abstract methods have to be either protected or public. They can not be private.
A class can implement multiple interfaces using implements keyword. A class can extend only one abstract class.
An interface can extend multiple interfaces using the extends keyword. An abstract class can only extend one other class.
Similarities:
  • Similar to abstract classes, we can not create objects of interfaces, but they can be used as references. Any object of the class implementing that interface can be assigned to that reference. See LINE A and LINE D below.
  • Every class implementing an interface has 'IS-A' relation with interface, so where ever we can use the interface, we can also use the class which implements it.
  • Similar to abstract classes, the abstract methods can be called using the interface references. See LINE B below.
  • As with classes extending abstract class, any class implementing an interface should implement all the methods of the interface. If it is not implementing all the methods, then that class should be marked as abstract.
interface IA
{
    void printA();
}

class A implements IA
{
    public void printA()
    {
        System.out.println("A's implementation of printA method.");
    }
}

...

IA ia = new A(); // Valid - LINE A
ia.printA();  // Valid - LINE B

A a1 = new A();
ia = a1; // Valid - LINE D

IA ia2 = new IA(); // INVALID
...
See Creating Interface In Java With Example Program to for more details about interfaces and their rules.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App