Skip to content

Commit b05ca18

Browse files
authored
Update Abstract vs Interface
1 parent d76739d commit b05ca18

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1+
Abstract keyword
12

3+
A. Abstract classes
4+
1. Abstract classes cannot be instantiated.
5+
2. The class extending the abstract class can create an instance of that abstract class.
6+
3. If a class has even one abstract method it should be declared as an abstract class.
7+
4. A abstract class may or may not have any abstract method.
8+
5. Abstract class defines what an abstract method does not how it is implemented.
9+
6. It is necessary to include all the method of abstract class in the first concrete class failing to do so
10+
the incomplete class should also be declared as an abstract class.
11+
7. We don't create a static abstract method in an abstract class as static methods cannot be overridden by subclasses.
12+
13+
B. Abstract Method
14+
1. Abstract method does not have the input argument or the implementation part, just the prototype.
15+
2. If a abstract method is present the class must also be declared as abstract.
16+
17+
Interface Keyword
18+
19+
1. All variables in interface are public static final.
20+
2. All methods are public abstract.
21+
3. The next concrete class implementing the interface should implement all its abstract method (simply all method)
22+
or it itself should be declared as an abstract class.
23+
4. An Interface is a 100% abstract class as each of its methods are abstract.
24+
5. An Interface can implement a class or extend an Interface.
25+
26+
27+
Abstract class vs Interface
28+
29+
1. Abstract class can have method with implementations while Interface cannot.
30+
2. Abstract class can have different access modifies while Interface cannot it can only public.
31+
3. A class can extend only one abstract class but implement many Interfaces
32+
33+
From the Oracle documentation
34+
35+
Consider using abstract classes if :
36+
37+
You want to share code among several closely related classes.
38+
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
39+
You want to declare non-static or non-final fields.
40+
Consider using interfaces if :
41+
42+
You expect that unrelated classes would implement your interface. For example,many unrelated objects can implement Serializable interface.
43+
You want to specify the behaviour of a particular data type, but not concerned about who implements its behaviour.
44+
You want to take advantage of multiple inheritance of type.
45+
abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.
46+
47+
Java 8 indroduced Default methods in Interface which allow the interfaces to have methods with implementation without affecting
48+
the classes that implement the interface.

0 commit comments

Comments
 (0)