Interface Vs Abstract Class After Java 8
With the introduction of concrete methods (default and static methods) to interfaces from Java 8, the gap between interface and abstract class has been reduced significantly. Now both can have concrete methods as well as abstract methods. But, still there exist some minute differences between them. In this article, we will try to list down the differences between interface Vs abstract class after Java 8.
Differences Between Interface And Abstract Class After Java 8 :
1) Fields
Interface fields are public, static and final by default. Interfaces still don’t support non-static and non-final variables. Interfaces can only have public, static and final variables. On the other hand, abstract class can have static as well as non-static and final as well as non-final variables. They also support private and protected variables along with public variables.
2) Methods
After Java 8, an interface can have default and static methods along with abstract methods. Interfaces don’t support final methods. But, abstract classes support final as well as non-final methods and static as well as non-static methods along with abstract methods.
Also note that, only interfaces can have default methods. Abstract classes can’t have default methods.
3) Constructors
Interfaces can’t have constructors. Abstract classes can have any number of constructors.
4) Member’s Accessibility
All members of interfaces are public by default. Interfaces don’t support private and protected members. But, abstract classes support all type of members – private, protected and public members.
5) Multiple Inheritance
A class can extend only one abstract class, but can implement multiple interfaces. Thus, a class can inherit multiple properties from multiple sources only through interfaces, not through abstract classes.
Interface Vs Abstract Class After Java 8 :
The below table and program summarizes the similarities and differences between interface and abstract class after Java 8.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| interface anyInterface { int i = 10 ; //By default, interface fields are public, static and final void abstractMethod(); //Interface can have abstract method default void defaultMethod() { System.out.println( "Interface can have default method" ); } static void staticMethod() { System.out.println( "Interface can have static method" ); } //No constructors in an interface //No non-static and non-final variables in an interface //No private fields and methods in an interface //No protected fields and methods in an interface //No final methods in an interface } abstract class anyAbstractClass { private int a; //Abstract class can have private field protected int b; //Abstract class can have protected field public int c; //Abstract class can have public field static int d; //Abstract class can have static field final int e = 10 ; //Abstract class can have final field int f; //Abstract class can have non-static and non-final field public anyAbstractClass() { System.out.println( "Abstract class can have constructors" ); } abstract void abstractmethod(); //Abstract class can have abstract method private static void staticMethod() { System.out.println( "Abstract class can have private and static method" ); } public void nonStaticMethod() { System.out.println( "Abstract class can have public and non-static method" ); } protected void protectedMethod() { System.out.println( "Abstract class can have protected method" ); } final void finalMethod() { System.out.println( "Abstract class can have final method" ); } //No default method in an abstract class } |
Comments
Post a Comment