Sage-Code Laboratory
index<--

OOP Abstraction

There are two things you can make abstract: A method and a class. Abstract methods are designed to be implemented later. Abstract classes are designed to be extended by other classes. You can not instantiate an abstract class and you can not execute an abstract method.

Abstract Methods

An abstract method is declared without an implementation. That is curly bracket block is missing. In addition you must use keyword "abstract" at the beginning of declaration. Let's see:

Example:

Next code fragment implements an abstract method that have only name, no parameters and no return value. Also, declaration is ending with ";". There is no implementation block {...} therefore it requires to be declared abstract.

//declare abstract method with no result
abstract void draw();

Note:By declaring one abstract method in a class, the class must be also abstract. It can not be used but to be extended by a descendent class.

Abstract Classes

An abstract class is declared using keyword "abstract". This kind of class do have a block for implementation but inside the block we do not have all the methods implemented. Some can be abstract methods to be implemented in a descendent class.

Example:

Next code fragment implements an abstract class that have inside an abstract method:

//declare abstract method with no result
public abstract class GraphicObject {
   // declare fields
   ...   
   // declare abstract methods
   abstract void draw();
   ...   
   // declare nonabstract methods
   ...
}

Read next: Interfaces