SCJP Study Guide:
OO Concepts


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Inheritance

Inheritance


In Object-oriented programming, objects are defined in terms of classes. A class describes the properties and the behaviors of an object. For example, a car has a steel wheel, driver seat, probably passenger seat, windows and doors. A car can accelerate and stop. A race car or a SUV is a special type of the car, but they both share the properties and the behaviors that we already described for a car. One of the most important concept in object-oriented design is that we can define more specific objects by extending the definition of the more general objects. This kind of extending or re-use is called Inheritance.

In object-oriented terminology, the car is the superclass, the race car and the SUV are the subclasses of the car. The relationship of the them can be show in the following figure:

Each of the subclasses inherits the properties and behaviors from the superclass. But the subclasses are not limited to the properties and the behaviors of the superclass. Each subclass can have more specific properties and the behaviors.

Further more, inheritance works for multiple levels or generations as well. That is, you can inherit from a parent to create a child. Then you can inherit from the child to create a grandchild. Any changes made to either the parent or child is reflected in the grandchild.

As you might imagine, this means that you have the ability to create intricate and complex structures or inheritance hierarchies. Inheritance is a powerful feature. But it s the kind of power that's easy to abuse and significantly more difficult to use well. What's the best strategy for creating an inheritance hierarchy? The first rule is to think in terms of generalization. The higher the class is in the hierarchy, the more general and abstract it should be. The lower you get in the class hierarchy, the more concrete and specific the classes should be.

In Java, the Object class is at the top of class hierarchy, and each class is its descendant (directly or indirectly). A variable of type Object can hold a reference to any object, such as an instance of a class or an array. Object provides behaviors that are shared by all objects running in the Java Virtual Machine. For example, all classes inherit Object's toString() method, which returns a string representation of the object. The Managing Inheritance section covers the Object class in detail.

Inheritance offers the following benefits:

  • Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times.
  • Programmers can implement superclasses called abstract classes that define common behaviors. The abstract superclass defines and may partially implement the behavior, but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

How do the accessors work with inheritance?


public and protected attributes (static or not) are inherited in all subclasses of the superclass

For example:

public class Car {
  public int driverSeat = 1;
  public static int steelWheel = 1;
  protected int speed;

  public void accelerate(int newSpeed) {
    this.speed = newSpeed;
  }

  public void stop() {
    this.speed = 0;
  }
}

public class SUV extends Car {
  public static void main (String [] args) {
    SUV suv = new SUV();
    System.out.println("number of steel wheel: " + suv.steelWheel);
	suv.accelerate(5);
	System.out.println("my speed: " + suv.speed);
  }
}

The SUV class inherits the steelWheel and speed attributes from the Car class. The output is:

number of steel wheel: 1
my speed: 5

private attributes (static or not) are not inherited in subclasses

For example:

public class Car {
  public int driverSeat = 1;
  public static int steelWheel = 1;
  protected int speed;
  private static boolean hasPassengerSeat = false;

  public void accelerate(int newSpeed) {
    this.speed = newSpeed;
  }

  public void stop() {
    this.speed = 0;
  }
}

public class SUV extends Car {
  public static void main (String [] args) {
    SUV suv = new SUV();
    System.out.println("I have a passengerSeat: " + suv.hasPassengerSeat);
  }
}

The above code has a compile time error, because Car.hasPassengerSeat is invisible to the subclass SVU.

A subclass attribute can shadow or hide the inherited attribute from its super classes

A subclass can inherit public and protected attributes from its superclasses. On the other hand, a subclass can also hide or shadow the inherited attributes from its super classes. For more details, refer to XyzWs Java FAQ: What is variable hiding and shadowing?

Inheritance of Interfaces


So far we have only discussed the inheritance of classes. In fact, inheritance works for interfaces as well. The difference between inheriting from a superclass and an interfaces are:

  • when inheriting from a superclass, use "extends"; when inheriting from interfaces, use "implements".
  • a class can inherit from more than one interface at the same time; while a class can only inherit from a single superclass at a time.
  • when inheriting from an interface, the class must implements all the methods that defined in the interface; while inheriting from a superclass, only override the methods that you want to provide a different definition.

For example, inherit from a superclass:

public class Parent {
  protected String name;
  public String myName(){
    return name;
  }
  public void printName(){
    System.out.println("Parent Name: " + name);
  }
}

public class Child extends Parent{
  public void printName(){
    System.out.println("Child Name: " + name);
  }
}

inherit from interfaces:

public interface Person {
  public void printName();
  public void printAge();
}

public interface Parent {
  public int numberOfChildren();
}

public class GrandParent implements Person, Parent {
  private String Name;
  private int age;
  private int numberOfChildren;
  public void printName() {
    System.out.println("Name: " + name);
  }

  public void printAge() {
    System.out.println("Age: " + age);
  }

  public int numberOfChildren() {
    return numberOfChildren;
  }
}

Refer to the SCJP Study Guide: Interfaces for information on interfaces.


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |