Abstract Classes in JAVA

 Abstract Classes


• Java allows abstract classes

– use the modifier abstract on a class header to declare an

abstract class

abstract class Vehicle

{ ... }


• An abstract class is a placeholder in a class hierarchy

that represents a generic concept

• An abstract class often contains abstract methods, though it

doesn’t have to

– Abstract methods consist of only methods declarations, without any

method body


• The non-abstract child of an abstract class must override the

abstract methods of the parent

• An abstract class cannot be instantiated

• The use of abstract classes is a design decision; it helps us

establish common elements in a class that is too general to

instantiate


Abstract Class: Example


public abstract class Vehicle

{

String name;

public String getName()


{ return name; } \\ method body


abstract public void move();


\\ no body!


}



Abstract Method


• Inheritance allows a sub-class to override the methods of its

super-class.


• A super-class may altogether leave the implementation details of a method and declare such a method abstract:


• abstract type name(parameter-list);


• Two kinds of methods:

1) concrete – may be overridden by sub-classes

2) abstract – must be overridden by sub-classes


• It is illegal to define abstract constructors or static methods.



Post a Comment (0)
Previous Post Next Post