It is a class that cannot be directly instantiated.
An abstract class
abstract
,static
, and
final
, private
, public
).abstract class GraphicObject {
int id;
GraphicObject(int id){
this.id = id;
}
void print(){
System.out.println("Printing a Graphic Object");
}
abstract void draw();
}
If a class includes abstract methods, then it must be declared abstract.
// THIS IS NOT ALLOWED, BECAUSE draw() IS NOT IMPLEMENTED
class GraphicObject {
abstract void draw();
// Remainder of class body...
}
A subclass of an abstract class usually provides implementations for all of its abstract methods. If it does not, the subclass itself must also be declared abstract.
abstract class Rectangle extends GraphicObject {
void print();
// Remainder of class body...
}
Abstract classes prevent a programmer from instantiating the base class, because a developer has marked it as having missing functionality. It also provides compile-time safety so that you can ensure that any classes that extend your abstract class provides the bare minimum functionality to work.
Abstract class
Interface