Abstract Class is a class that can't be instantiated (objects cannot be created) and may contain abstract methods.
It's supposed to represent something abstract and not specific.
Let's look at the following example:
Output:
Explanation
Vehicle
is defined at lines 2-7
.Vehicle
.
This is something indefinite and it's inherited by other classes.line 22
the code won't
compile. That's because an abstract class cannot be instanced.Abstract methods can be defined only in abstract classes and have no body.
An abstract method in parent class must be overwritten in the child class.
Syntax:
Example:
Output:
Explanation:
line 4
the abstract method
calcArea
is defined.
calcArea
is an abstract method, classes that
inherit the abstract class Figure
must implement
the method. If not done then the code won't execute.
lines 17 - 20
and lines 35 - 38
the abstract method is implemented.