-
Notifications
You must be signed in to change notification settings - Fork 55
/
Abstract Class Vs Interface
41 lines (33 loc) · 2.4 KB
/
Abstract Class Vs Interface
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
VERY IMPORTANT POINT:
A normal class cannot have abstract methods. If a method in a normal class is declared as abstract, then the
whole class has to be declared as abstract. Otherwise compilation and runtime error would take place.
Interfaces:(Datamembers: public static final, Methods: public abstract, Object cannot be created)
a) Cannot be instantiated
b) ALL datamembers(NOT METHODS) BUT NOT METHODS are "public static final"
3) All methods are "public abstract" (AND THEY ARE NEITHER static NOR final SINCE THE CLASS IMPLEMENTING THIS
INTERFACE HAS TO COMPULSORILY DEFINE THE METHODS BY OVERRIDING THEM. HENCE THEY CANNOT BE STATIC NOR FINAL
BECAUSE STATIC OR/AND FINAL METHODS CANNOT BE OVERRIDDEN)
4) Each and every method dont have a defination
5) Classes which implement the Interface have to define the abstract methods
6) By default, ALL METHODS in an interface are decleared as public, abstract. It will never be static.(BECAUSE
STATIC METHODS CANNOT BE OVERRIDDEN)
Abstract Classes:(Methods: can or cannot be public abstract, Object cannot be created)
1) Cannot be instantiated
2) We cannot create instance for an abstract class. We can able to create instance for its subclass only.
By specifying abstract keyword just before class, we can make a class as abstract class.
public abstract class MyAbstractClass{
}
3) Abstract class may or may not contains abstract methods. Abstract method is
just method signature, it does not containes any implementation. Its subclass
must provide implementation for abstract methods. Abstract methods are looks
like as given below:
public abstract int getLength();
4) Methods can have defination. Some of the methods can be defined while some of the methods can be abstract.
However an abstract class can have NO abstract methods. Even if a class does not have abstract methods but
is declared as abstract is said to be an abstract class.
5) We have to explicitly declare the abstract methods as "public abstract" so that they can be defined by the
classes which inherit the abstract class.
6) Abstract classes fall in between "Interfaces" and proper actual defined "Classes" since they have both the
properties.
7) A normal class cannot have abstract methods. If a method in a normal class is declared as abstract, then the
whole class has to be declared as abstract. Otherwise compilation and runtime error would take place(VERY IMPORTANT POINT).