Differences between Abstarct class and Interface complete tutorial with suitable examples and sample outputs. In case if you need more information about the difference between Abstract class and Interface leave a comment here.
An abstract class can have concrete methods, but an interface cannot have.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
abstract class ABC { void one() { //this is valid } void two() { //this is valid } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
interface INT { void xyz() { //this is invalid } void abc() { //this is invalid } } |
Example 2:
An abstract class can consist both abstract methods and concrete methods but the interface does not consist of any concrete methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
abstract class one { abstract void xyz(); // abstract method void xyz1(int x,int y) // concrete method { x=1;y=2; System.out.println(x+y); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
interface one { void xyz(); // abstract method void xyz(int x); //abstract class void xyz1(int x,int y): // concrete method not valid in interface { } } above code has an error called abstract method do not contain body( { } ) if we remove that body and place semicolon it will compile successfully. |
An Abstract class can have a constructor, but an interface cannot.
Variables in an abstract class can be normal or final, but in an interface, the variables should always be final. So they must be initialized when declared.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
abstract class ABC { int a=10; //valid int b; //valid } interface INT { int a=10; //valid int b; //invalid } |
Variables in an abstract class can be public, protected, normal or private. But variables in an interface are always public. If we don’t specify any, then the system will treat it as public only by default.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
abstract class ABC { public int a=1; //valid protected int b=1; //valid int c=1; //valid private int d=1; //valid } interface INT { public int a=1; //valid protected int b=1; //invalid int c=1; // valid (by default, variable in an interface is public) private int d=1; //invalid } |
Variables in an abstract class can be instant or static, but in an interface, they are static by default.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
abstract class ABC { //a is instance variable, b is static int a=1; static int b=2; } interface INT { //both a and b are static int a=1; static int b=2; } class Check { public static void main(String arg[]) { int x; x=ABC.a; // not valid as a is instance variable in ABC x=ABC.b; //valid x=INT.a; //valid x=INT.b; //valid } } |
An abstract class can implement an interface, but an interface cannot implement any other interface. An abstract class can implement multiple interfaces.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
interface ONE { } abstract class ABC implements ONE //valid { } interface INT implements ONE //invalid { } interface TWO { } abstract class ABSTRACT implements ONE,TWO //valid { } |
An abstract class cannot extend multiple abstract classes. An interface can extend multiple interfaces. An abstract class cannot extend multiple classes also. It means multiple inheritances are not allowed with abstract classes but allowed with interfaces.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
abstract class First { } abstract class Second { } abstract class ABC extends First,Second //invalid { } interface ONE { } interface TWO { } interface INT extends ONE,TWO //valid { } |
When an abstract class extends another abstract class, the abstract class can implement the methods of the parent abstract class or leave it without implementing. But an interface can never implement anything.
When we want to implement some mechanism and leave the remaining to child classes then abstract classes are preferred. When we want all the implementation in the child classes only then interfaces are used.
Similarities
- Both can have abstract methods.
- The object cannot be created for both.
- Reference can be created for both.
- Generally, the reference is used to refer to the object of a child class.
When a class in extending an abstract class, that class should override all methods of the abstract class. Similarly, when a class is implementing an interface, that class should override all methods of the interface.
In both the cases, if the class does not override all the methods, then the class should be declared as abstract.
An abstract class can be extended by any number of classes and an interface can be implemented by any number of classes.