Java Programming/Keywords/abstract

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

abstract is a Java keyword.

It declares a class abstract, not all its methods are defined/implemented. Objects cannot be created from an abstract class. It needs to have a non abstract subclass in order to create an object.

It also declares a method abstract, that is, not implemented in the abstract class. A method cannot be declared abstract in a non abstract class.

Syntact:

public abstract ClassName
or
abstract public ClassName

and for methods in an abstract class :

public abstract void methodName();  // --- No body, no implementation ---
or
abstract public void methodName();  // --- No body, no implementation ---


For Example:

public abstract ClassName
{
   // --- This method does not have a body; it is abstract. ---
   public abstract void abstractMethod();

   // --- This method does have a body; it is implemented in the abstract class; gives a default behavior. ---
   public void normalMethod()
   {
       ...
   }
}