Inheritance and Abstract Classes

Page last modified 22:02, 16 Dec 2008 by AtP | Page History

The Concept of Inheritance

screen1.pngThe idea behind inheritance is specialization. For example, suppose you have a class representing an employee and a class representing a manager. In this case, the manager is a specialized type of employee and thus the class manager inherits from the class employee.

Inheritance is a relationship model between classes where one class, called superclass, represents a general concept and one class, called subclass, represents a more specialized concept. The subclass inherits from the superclass which means:

  • it has the same methods as the superclass,
  • it is possible to override methods of the superclass,
  • you cannot access instance fields of the superclass in the subclass,
  • and you can add additional instance fields and methods to the subclass.

Specialization:

  • object is member of ONE class only
  • no multiple inheritance in Java => single specialization of disjunct subclasses
  • superclass might or might not have objects

Inheritance hierarchies

In the real world, Hierarchies describe general and specific relationships. You can present them as a tree with the general concept at the root of the tree and the more specific concepts as children nodes. In programming an inheritance hierarchy can be represented with a tree which has the general superclass at its root and more specific subclasses as children nodes. Grouping classes into hierarchies is a common principle in object oriented design.

Substitution principle (Barbara Liskov)

You can use a subclass object whenever a superclass object is expected.

Example:

Employee e = new Manager(); 
e.getSalary();

If a subclass overrides a method of the superclass, the Java virtual machine decides which method is used depending on the type of the object. So, in this example, the getSalary method of the manager class is used. This principle is called polymorphism.

The super keyword

Example:

public double getSalary()   {     return super.getSalary() + bonus;    }       

The super keyword is used to access methods of the superclass inside methods in the subclass. Another use of this keyword is to access the constructor of the superclass.

Example:

public Manager( String aName )
{
super(aName);
bonus = 0;
}

The super keyword must be the first command in the constructor.

Preconditions and Postconditions

  • preconditions of a subclass method cannot be stronger than those of the superclass method
  • postconditions of a subclass method must be at least as strong as those of the superclass method

Visibility and exceptions

  • a public method cannot become private, but a private method can become public
  • a subclass method cannot throw more exceptions than the superclass method

Abstract classes

An abstract method is an undefined method which must be defined in a subclass. A class containing one or more abstract methods must be declared as abstract and is thus called an abstract class. An abstract class can have instance fields. It is not possible to create objects of an abstract class. 

Abstract classes versus interface types

abstract class
interface
can have instance fields no instance fields, only constants
can define methods no method definitions
inheritance from only one class implementation of multiple interface types

The Template Method Pattern

Context:

  • An algorithm is applicable for multiple types.screen2.png
  • The algorithm can be broken down into primitive operations.
  • The primitive operations can be different for each type.
  • The order of the primitive operations does not depend on the type.

Solution

  • define superclass with
    • one method for the algorithm
    • abstract methods for the primitive operations.
  • implement the algorithm to call the primitive operations in the appropriate order.
  • do not define the primitive operations in the superclass, or define them to have appropriate default behavior.
  • each subclass defines the primitive operations (but not the algorithm).

 

Protected interfaces

The protected keyword allows access to a method of a superclass from all subclasses of this class and from classes of the same package. The idea behind using protected methods is to provide a protected interface and a public interface. The protected interfacedoes only contain operations which are made for the subclasses. The public interface is made for the clients of the superclass (other program parts / packages using it).

Example:

CompoundShape (superclass):

import java.awt.*;
import java.awt.geom.*;

public abstract class CompoundShape
{
private GeneralPath path;

public CompoundShape()
{
path = new GeneralPath();
}

protected void add( Shape s )
{
path.append( s, false );
};
}

CarShape (subclass):

public class CarShape extends CompoundShape 
{
public CarShape( int x, int y, int width, int height )
{
Rectangle2D.Double base = ...;
add( base );
}
}

The intention of using the add method in the CarShape's constructor is that it allows access to the private instance field path of the superclass CompoundShape.

Someone could argue that it is possible to declare the add method as public but in that case, clients of the class could accidentally add not fitting shapes.

However, protected fields should not be used because they have the same disadvantages as public fields.

When not to use inheritance

  • if two classes have has-a relationship
    • example: a class circle has points, it is not a point
  • do not use inheritance if it violates the substitution principle
    • example: the class stack in the java.util package which extends a dynamic array class although it has no use for some methods of a dynamic array
      • for instance, removing an element of a stack given its index
Tag page
Pages that link here
Page statistics
700 view(s), 12 edit(s), and 8455 character(s)

Comments

You must login to post a comment.

Attach file

Attachments