Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 23: Inheritance and overriding inherited methods

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Inheritance and overriding inherited methods


Overview

Object-oriented design implements two simple relationships:

"has a" and "is a"

You have already seen many examples of the "has a" relationship. It is implemented by the instance variables of a class. For example, consider the following class for maintaining and processing certain Vehicle data:

class Vehicle {
private String description;
private int capacity;
private double maxSpeed;

public Vehicle(String iDescription, int iCapacity, double iMaxSpeed) {
setDescription(iDescription);
setCapacity(iCapacity);
setMaxSpeed(iMaxSpeed);
}
public void setDescription(String nDescription) {
description = nDescription;
}
public boolean setCapacity(int nCapacity) {
if (nCapacity >= 0) {
capacity = nCapacity;
return true;
}
else
return false;
}
public boolean setMaxSpeed(double nMaxSpeed) {
if (nMaxSpeed > 0) {
maxSpeed = nMaxSpeed;
return true;
}
else
return false;
}
public String getDescription() {
return description;
}
public int getCapacity() {
return capacity;
}
public double getMaxSpeed() {
return maxSpeed;
}
}

According to this class definition, a Vehicle "has a" description, "has a" passenger capacity, and "has a" maximum speed. Notice that the relationship can be implemented by either a primitive variable or a reference to an object of another class (a String object is used to maintain the Vehicle description).

While the "has a" relationship is implemented through the instance variables of a class, the "is a" relationship is implemented by extending an existing class.

Extending a class

  • Defines a new class that "is a" more specific version of the class being extended. For example, a Bicycle class can be defined that extends the Vehicle class. After all, a Bicycle "is a" Vehicle. As an extension of Vehicle, the Bicycle class would inherit all the features of the Vehicle class and could add additional features.

The class hierarchy would actually be

Object

Vehicle

Bicycle

indicating that a Bicycle "is a" Vehicle and a Vehicle "is an" Object. As a custom class, the Vehicle class is automatically an extension of Java's Object class and inherits all its features. The Object class is the "root" of the class hierarchy.

  • Is sometimes called "subclassing". The class being extended is referred to as the "superclass" (or parent) and the new class is referred to as its "subclass" (or child). A class can be both a subclass and a superclass. In the above class hierarchy, the Vehicle class is a subclass of Object and a superclass of Bicycle.

In Java, a class can never have more than one superclass. This is called "single inheritance" and is a fundamental difference between Java and C++ (which permits "multiple inheritance).

While a Java class can have only one superclass, it can be extended to have any number of subclasses. For example, the Vehicle class can be extended to create classes such as AirPlane, Automobile, HotAirBalloon, OxCart, etc.

A class inherits all features of the classes above it in the class hierarchy. Such classes are referred to as its "ancestors".

  • Requires the use of the extends keyword. For example, if the Bicycle class is to extend the Vehicle class, its class would be coded as follows:

class Bicycle extends Vehicle

  • Has an impact on how an object of the class is constructed. During instantiation, the constructor methods of all ancestor classes must be executed. For example, a Bicycle object depends upon its inherited Vehicle class and Object class features being properly initialized.

To accomplish this, the first thing each subclass constructor MUST do is call its superclass constructor as shown by this diagram:

Bicycle

Vehicle

Object

constructor

constructor

constructor

1) Call superclass constructor

-->

1) Call superclass constructor

-->

Complete construction

2) Complete construction

<--

2) Complete construction

<--

The call to the default (no argument) superclass constructor is automatically generated by the Java compiler. This makes it easy to define a first-generation custom class, such as Vehicle. It needn't be concerned with calling the constructor of the Object class because it happens automatically.

When extending a class, it is necessary to call a superclass constructor that accepts parameters. To do so, the call must be explicitly coded as the first statement within the constructor. The syntax of the call is as follows:

super(arguments);

The keyword super always references the immediate superclass of the current object. When used as a method name, it indicates a call to the superclass constructor. Based upon the arguments being passed, the correct overloaded superclass constructor will be called.

  • Example: The following program defines a Vehicle class, a Bicycle class that extends the Vehicle class, and a trivial application that constructs and acts upon a Bicycle object.

public class App {
public static void main(String[] args) {
Bicycle bike = new Bicycle("Mary's old bike", 1, 15.5, 14, true);
Utility.skip();
System.out.println(" Description: " + bike.getDescription());
System.out.println(" Wheel diameter: " + bike.getWheelDiameter());
System.out.println(" Top speed: " + bike.getMaxSpeed());
bike.setDescription("Mary's modified bike");
bike.setWheelDiameter(1Cool;
bike.setMaxSpeed(22.5);
Utility.skip();
System.out.println(" Description: " + bike.getDescription());
System.out.println(" Wheel diameter: " + bike.getWheelDiameter());
System.out.println(" Top speed: " + bike.getMaxSpeed());
Utility.skip();
System.out.println(" Object information: " + bike.toString());
}
}

class Vehicle {
private String description;
private int capacity;
private double maxSpeed;
public Vehicle(String iDescription, int iCapacity, double iMaxSpeed) {
setDescription(iDescription);
setCapacity(iCapacity);
setMaxSpeed(iMaxSpeed);
}
public void setDescription(String nDescription) {
description = nDescription;
}
public boolean setCapacity(int nCapacity) {
if (nCapacity >= 0) {
capacity = nCapacity;
return true;
}
else
return false;
}
public boolean setMaxSpeed(double nMaxSpeed) {
if (nMaxSpeed > 0) {
maxSpeed = nMaxSpeed;
return true;
}
else
return false;
}
public String getDescription() {
return description;
}
public int getCapacity() {
return capacity;
}
public double getMaxSpeed() {
return maxSpeed;
}
}


class Bicycle extends Vehicle {
private int wheelDiameter;
private boolean hasBell;
public Bicycle(String iDescription, int iCapacity, double iMaxSpeed,
int iWheelDiameter, boolean iHasBell) {
super(iDescription, iCapacity, iMaxSpeed);
setWheelDiameter(iWheelDiameter);
setHasBell(iHasBell);
}
public boolean setWheelDiameter(int nWheelDiameter) {
if (nWheelDiameter >= 12 && nWheelDiameter <= 26) {
wheelDiameter = nWheelDiameter;
return true;
}
else
return false;
}
public void setHasBell(boolean nHasBell) {
hasBell = nHasBell;
}
public int getWheelDiameter() {
return wheelDiameter;
}
public boolean hasBell() {
return hasBell;
}
}

Notes:

  1. When the application instantiates the Bicycle object, the Bicycle constructor passes the description, capacity, and maximum speed through to the superclass (Vehicle) constructor.

  2. As the application acts upon the Bicycle object (bike), it calls the object's instance methods without regard to their class. For example, the getDescription() method is in the Vehicle class, the setWheelDiameter() method is in the Bicycle class, and the toString() method is in the Object class. The inherited toString() method isn't very useful, but we are about to change that.

Overriding an inherited method

  • Requires the definition of a method having the same name, same arguments, and same return type as the inherited method. The newly defined method hides the overridden method so that calls to the method go to the new version and not the overridden version.

For example, all classes inherit the toString() method of the Object class. The default behavior of the method is to return a String containing general information about the object (its class name and the value of its object reference). To override the inherited method to provide other information, a class could contain the method

public String toString() {
custom code goes here...
}

where the custom code would build and return a String to the caller.

  • Is NOT to be confused with overloading. Overloading requires that the methods must differ in their signature (arguments). Overriding requires that the method headers be identical.

  • Example: The following program is a slightly modified version of the previous example. The only change is the overriding toString() method in the Bicycle class.

public class App {
public static void main(String[] args) {
Bicycle bike = new Bicycle("Mary's old bike", 1, 15.5, 14, true);
Utility.skip();
System.out.println(" Description: " + bike.getDescription());
System.out.println(" Wheel diameter: " + bike.getWheelDiameter());
System.out.println(" Top speed: " + bike.getMaxSpeed());
bike.setDescription("Mary's modified bike");
bike.setWheelDiameter(1Cool;
bike.setMaxSpeed(22.5);
Utility.skip();
System.out.println(" Description: " + bike.getDescription());
System.out.println(" Wheel diameter: " + bike.getWheelDiameter());
System.out.println(" Top speed: " + bike.getMaxSpeed());
Utility.skip();
System.out.println(" Object information: " + bike.toString());
}
}

class Vehicle {
private String description;
private int capacity;
private double maxSpeed;
public Vehicle(String iDescription, int iCapacity, double iMaxSpeed) {
setDescription(iDescription);
setCapacity(iCapacity);
setMaxSpeed(iMaxSpeed);
}
public void setDescription(String nDescription) {
description = nDescription;
}
public boolean setCapacity(int nCapacity) {
if (nCapacity >= 0) {
capacity = nCapacity;
return true;
}
else
return false;
}
public boolean setMaxSpeed(double nMaxSpeed) {
if (nMaxSpeed > 0) {
maxSpeed = nMaxSpeed;
return true;
}
else
return false;
}
public String getDescription() {
return description;
}
public int getCapacity() {
return capacity;
}
public double getMaxSpeed() {
return maxSpeed;
}
}


class Bicycle extends Vehicle {
private int wheelDiameter;
private boolean hasBell;
public Bicycle(String iDescription, int iCapacity, double iMaxSpeed,
int iWheelDiameter, boolean iHasBell) {
super(iDescription, iCapacity, iMaxSpeed);
setWheelDiameter(iWheelDiameter);
setHasBell(iHasBell);
}
public boolean setWheelDiameter(int nWheelDiameter) {
if (nWheelDiameter >= 12 && nWheelDiameter <= 26) {
wheelDiameter = nWheelDiameter;
return true;
}
else
return false;
}
public void setHasBell(boolean nHasBell) {
hasBell = nHasBell;
}
public int getWheelDiameter() {
return wheelDiameter;
}
public boolean hasBell() {
return hasBell;
}
public String toString() {
String result = "[ " + getDescription() + ", " +
getCapacity() + ", " +
getMaxSpeed() + ", " +
getWheelDiameter() + ", " +
hasBell() +
" ]";
return result;
}

}

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

Review questions

  1. You have been given the specification for a personnel system to be implemented in Java. Part of it states:

"An employee has an employee number, a name, and a hire date. A sales representative is an employee who has a commission rate and a district."

Given that the Employee class has already been defined, code the class header of the SalesRep class so that it can be referenced from any class.

  1. You have been given the specification for a personnel system to be implemented in Java. Part of it states:

"An employee has an employee number, a name, and a hire date. A sales representative is an employee who has a commission rate and a district."

Given that the Employee class has already been defined, which of the following fields would be appropriate for inclusion in the SalesRep class as members? (choose two)

  1. Employee theEmployee;

  2. double commissionRate;

  3. String district;

  4. String name;

  5. Date hireDate;

  1. Assume that class Nothing has been defined with a single constructor that accepts a String. Which one of the following legally define a class that extends Nothing?

  1. class DoesNothing extends Nothing {
    private int value;
    public DoesNothing(String iName, int iValue) {
    super(iName);
    value = iValue;
    }
    }

  2. class DoesNothing extends Nothing {
    private int value;
    public DoesNothing(String iName, int iValue) {
    value = iValue;
    super(iName);
    }
    }

  3. class DoesNothing extends Nothing {
    private int value;
    public DoesNothing(String iName, int iValue) {
    Nothing(iName);
    value = iValue;
    }
    }

  4. class DoesNothing extends Nothing {
    private int value;
    public DoesNothing(String iName, int iValue) {
    super();
    value = iValue;
    }
    }

  5. class DoesNothing extends Nothing {
    private int value;
    public DoesNothing(String iName, int iValue) {
    int y = 5;
    super(iName);
    value = iValue * y;
    }
    }

  1. Assume that a class inherits a single method named calcAmount that has the following method header:

public double calcAmount(double discount, boolean preferred)

Which of the following is the header of a method that can legally be coded within the class?

  1. public float calcAmount(float d, boolean p)

  2. public double calcAmount(double d)

  3. public double calcAmount(double d, boolean p)

  4. public float calcAmount(float d)

  5. all of the above


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy