Abstract Classes
Abstract Classes
Classes
■ An Abstract class is a class that is declared
abstract—it may or may not include abstract
methods. But its objects cannot be created.
■ An Abstract method is a method that is declared
without an implementation i.e. no body(without
braces, and followed by a semicolon), like this:
abstract void add(int X, int Y);
■ If a class includes abstract methods, the class
itself must be declared abstract, as in:
public abstract class Shape
{ // declare properties
// declare non-abstract methods
abstract void area();
}
■ When an abstract class is sub classed, the
subclass usually provides implementations for all
of the abstract methods in its parent class.
However, if it does not, the subclass must also be
declared abstract.
Example
abstract class Shape
{
int x, y;
void accept(int X, int Y) //concrete method
{ ... }
abstract void area(); //abstract method
abstract void perimeter(); //abstract method
}
Each non-abstract subclass of Shape, such as Circle
and Rectangle, must provide implementations for
the area( ) and perimeter( ) methods.
Sub class Circle