Chapter 11: Inheritance and Polymorphism

Chapter 11: Inheritance and Polymorphism

Chapter 11: Inheritance and Polymorphism

11.2 Superclasses and Subclasses

Java is an object-oriented language. Everything (almost) is an object. What is an object? It is a collection of data (properties) and code (methods).

Create a new project called Chapter11. Add the following code:

Object x = new Object();

This creates an Object. Look at x's methods by typing x followed by a period.

Now create a new class and add it to your project:

public class MyClass{

publicMyClass()

{

}

}

Now add this code:

MyClass m = new MyClass();

Look at the properties and methods of m. They are the same as for x!

This is because all objects in Java inherit features from the Object superclass.

Every class that you create in Java is a subclass of the Object superclass.

Every class that you create in Java inherits features (properties and methods) from the Object superclass.

Every class that you create in Java extends the Object superclass.

Inheritance allows programmers to re-use and extend existing classes.

Look at the toString(),hashcode(), and equals() methods.

Have your program print out the toString() and hashcode() values. One is in hex and the other is in decimal. Convert the hex value to decimal:

Integer.toHexString(x.hashCode())

They should be the same!

Consider an Employee class. There might be different information that you might want to keep track of for different types of employees (e.g. salaried vs. hourly), but many things that all employees have in common. The things that all employees have in common would go in the Employee class. The things that apply only to salaried employees could go in a SalariedEmployee class that extends and inherits from the Employee class. And the things that apply only to hourly employees could go in anHourlyEmployee class that extends and inherits from the Employee class.

Wemighthaveanundergraduatestudentandagraduatestudent.Bothhavemanythingsincommon,butwemaywanttokeeptrackofafewdifferentthingsforgraduatestudentsthanwedoforundergraduatestudents. We could create a Student class that has all of the things that apply to all students. The things that apply only to undergraduate students could go in an UndergraduateStudent class that extends and inherits from the Student class. And the things that apply only to graduate students could go in a GraduateStudent class.

Anexamplefromthetextbook(11.2)isthatofageometricobject.Allgeometricobjectsmayhavesomecommonfeatures:

  • Istheobjectfilled?
  • Whatcoloristheobject?
  • Whenwastheobjectcreated?(Thisisastretch,Ithink)

However, a circle is different from a rectangle.

A circle is defined by its radius. A rectangle is defined by its length and width. While we would want to know what color the circle is and whether it is filled or not, we do not need to know its length and width.

And while we might want to know what color a rectangle is and whether it is filled or not, we do not need to know its radius.

We can use a GeometricObject class as a base class and we can extend it in one case to add a radius for a circle and we can extend it in another case to add the length and width of a rectangle.

Look at the files (there are links to them on our home page):

  • SimpleGeometricObject.java
  • CircleFromSimpleGeometricObject.java

Note the keyword extends on the first line of the CircleFromSimpleGeometricObject definition. This tells the compiler that the CircleFromSimpleGeometricObject class is based on the SimpleGeometricObject class and that it extends it by adding those things that are required for a circle.

The SimpleGeometricObject class is called a base class. And the CircleFromSimpleGeometricObject class is called a derived class. Sometimes (as in our textbook) the base class is called a super class and the derived class is called a subclass. I don't like these terms because a "super" class sounds "bigger" to me than a "sub" class. But it's actually the other way around. A subclass, because it adds more features to the super class, is "bigger" (has more code) than the super class. Base class and derived class just make more sense to me.

Add the SimpleGeometricObject class and the CircleFromSimpleGeometricObject class to your Chapter 11 project.

Examine both files.

The keyword extends tells the compiler that we are extending (and inheriting from) the SimpleGeometricObjectclass.

Look at the default constructor for the circle class.

Look at the first parameterized constructor for the circle class.

Try to set the value of color or filled from the constructor. You can't! They are private. However, you can use the "set" methods to set their values, because the "set" methods are public.

In-class exercise

Write a Rectangle class that extends the SimpleGeometricObjectclass. You should be able to initialize it with a constructor that accepts the length and width, with a constructor that accepts its length, width, color (string), and whether it is filled or not (Boolean). You should be able to get and set its length and width, determine its area, determine its perimeter, get its value as a string, and determine if it is equal to another rectangle.

11.3 Using the super Keyword

When in a derived (sub) class, use the word super to refer to the base (super) class. This is usually used for:

  1. To call a constructor in the base (super) class.
  2. To call a method in the base (super) class.

To call a base (super) class constructor:

  • super()
  • super(parameter list)

When calling super, you must make it the first instruction in the constructor.

11.4 Overriding Methods

To override a method, the derived class must write a method with the same signature as the method in the base class. The method in the base class cannot be declared as private. Anything that is declared to be private is not visible outside of the file in which it is declared.

The book has an example of overriding a method on page 418 (Liang9).

11.5 Overriding vs. Overloading

Overloadingrefers to having the same method name but with different parameter lists (different signatures).

Overriding refers to having the same method name with the same parameter list, but in a derived (sub) class.

The book has an example of overriding vs. overloading on page 419 (Liang9).

11.6 The Object Class and its toString() Method

All objects inherit from a single base class called the Object class. One of the Object class's methods is the toString method. However, it doesn't do what you might expect. It merely gives the name of the class, followed by an "at" symbol (@), followed by the memory address of the object. None of these are useful. You should always write your own toString method.

11.7 Polymorphism

Skip for now.

11.8 Dynamic Binding

Skip for now.

11.9 Casting Objects and the instanceof Operator

Skip for now.

11.10 The Object's equals Method

11.11 The ArrayList Class

Coveredin chapter 25.

11.12 A Custom Stack Class

Coveredin chapter 25.

11.13 The protected Data and Methods

11.14 Preventing Extending and Overriding

10/4/2018ChapterXX--Inheritance.docx1 of 5