Abstract Class
Abstract Class
An abstract class is a class that cannot be instantiated.
The class may contain any or none of the following:
- Ordinary methods. That is, class methods and/or instance methods.
- Abstract methods
- Variables - class variables and/or instance variables.
- Constants.
The purpose of an abstract class is to list items that are common to its
sub-classes. An abstract class must be declared with the keyword
abstract. An abstract cannot be instantiated. However, you
can declare a variable of such a class.
The format for declaring an abstract class is as follows:
public abstract class name
{
:
public abstract return_type method();
//Definition cannot be supplied.
:
}
Abstract Method
An abstract method is one that is declared but its body is not
implemented. The following rules govern the way one uses an abstract
method:
Relationship between an Abstract class and its direct sub-class
- The direct sub-class of an abstract class promises implement all
of the abstract methods that it has inherited from its direct super-class,
the abstract class. In other words, once a class has been declared to be
an abstract class, its direct subclass is expected to implement its
abstract method(s).
- If a direct subclass does not implement all of the abstract methods
that it inherited from an abstract class, then that sub-class too is an
abstract class. In this case the sub-class must be declared an abstract
class following the format given above.
- It is an error if a direct subclass fails to implement all of the
abstract methods that it inherited from its direct superclass, when at the
same time the subclass was not declared to be an abstract class.
- If a sub-class does not require all of the methods in an abstract
class, then one could form a concrete class from the abstract class, by
supplying dummy bodies for each method. From this point any class that
inherits this new class can use only those methods that are necessary for
that class.
- A non-abstract method that is defined within an abstract class can be
redefined in subsequent sub-class(s). These sub-classes do not
necessarily have to be direct subclass of the parent abstract class.
Demo Program No. 3
holiday.java
attraction.java
symphony.java
movie.java
myMovie.java
deadMovie.java
Go
Back