Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.5. Abstract classes

4.5. Abstract classes
Last updated: 27 January 2013.

An abstract class is a class that contains one or more methods that are declared without being implemented. Those methods are called abstract methods and only their signatures are defined. The idea is to let the subclasses implement the abstract methods. An abstract class cannot be instantiated. It can only be subclassed. For example, the abstract class Aircraft shown below is a simplified version of the class Aircraft used in the previous tutorials. It has a single abstract method land and an implemented method takeOff:

public abstract class Aircraft {

   
float length;
   
float height;
   
int range;
   
double weight;
   
boolean airborne;

   
public abstract boolean land();

   
public boolean takeOff(){
         
if(airborne == false){
               
airborne = true;
               
return true;
         
}
         
return false;
   
}

}

The subclasses of the class Aircraft must implement the method land. For example, the class Plane shown below inherits from the class Aircraft:

public final class Plane extends Aircraft {

   
public boolean land(){
         
// The implementation goes here
   
}

}

A subclass that does not implement one or more abstract methods of its super class must also be declared abstract.

Abstract classes are useful when you want to fully implement several functionalities in a class and only declare other functionalities (without implementing them) that will be implemented by subclasses.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.5. Abstract classes
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.6. Interfaces

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7