Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: Hidden Figure - May 24, 2019

AP Computer Science :: Lessons :: Inheritance and Polymorphism

Barron's AP Computer Science

Chapter 3:
Pages 186 - 196


Fundamentals of Java
Chapter 10
Pages 367 - 371

Inheritance

A superclass is a class that is used to generally describe something while a subclass describes something more specifically. For example, a superclass could be a blueprint for any house while a subclass of that blueprint may be a ranch home. Inheritance describes the relationship between these objects that share characteristics. Inheritance is useful because you can create a superclass that contains many methods that will be used by a number of different subclasses. This saves you from having to rewrite commom methods in each different class. Below is one example of an inheritance hierarchy.

Food Inheritance

Everything below Food is a subclass of the class above it. Food is the main superclass and all the subclasses below it are types of Food. You do not have to have a straight inheritance hierarchy like the image above. You could also have something like the following, which shows an actual example from BlueJ.

Java Inheritance

The arrows are pointing from a subclass to a superclass. Person is the main superclass while Student is a superclass for GradStudent and UnderGrad. These can also be referred to as is-a relationships. An Employee

is-a

Person and a Student

is-a

Person as well. The opposite is not always true, however. A Person may not be a Student and a Person may not be an Employee. Since an UnderGrad

is-a 

Student, however, an UnderGrad

is-a

Person by the transitive property.

Polymorphism

You will notice the arrows indicating the relationships between the classes in the example. The arrows can be created in BlueJ, or you can simply add extends superclass to the end of any subclass class declaration where superclass is the name of the superclass that the subclass inherits from. You will notice the extends keyword in the GradStudent and UnderGrad classes.

Polymorphism

A subclass inherits any public variables and methods from its superclass. It will also inherit any protected variables and methods. Classes that are on the same level as each other (such as GradStudent and UnderGrad) do not inherit anything from each other so UnderGrad will not have any access to the myGradID variable.

Methods in superclasses can be overridden by methods in subclasses if the methods have the same return type, name, and parameter types. In the example file, computeGrade in UnderGrad overrides the same method in Student. This means the code in the Student method will not run at all.

In the case of the GradStudent computeGrade method, however, it has a call to super.computeGrade(), which runs the code in the Student computeGrade method. When a method is written that overrides another method but also calls that method the superclass method is said to be partially overridden. A method that has been overridden in at least one subclass, such as the computeGrade method in Student, is said to be polymorphic. Polymorphism is used to select the appropriate method for a particular object in a class hierarchy.

It is also important to know that constructors are never inherited. If a constructor is not written for a subclass it will invoke the superclass' default constructor with no parameters. If a default constructor with no parameters doesn't exist in the superclass a compile-time error will occur. The superclass constructor can be called using the super() statement, with parameters if necessary, but it must be the first line in the subclass constructor. Here are some rules for subclasses:

When a superclass variable is defined it can refer to the superclass as well as any of its subclasses. Each of the following declarations would be legal:

Student s = new Student();
Student g = new GradStudent();
Student u = new UnderGrad();

The code above works because GradStudent

is-a

Student and UnderGrad

is-a

Student. Remember, though, that a Student is not necessarily a GradStudent or an UnderGrad student so the following would not be valid:

GradStudent g = new Student();
UnderGrad u = new Student();

The code s.setID() and u.setID() will also be invalid because the setID method is only available to the GradStudent class. The following code would cause an error as well.

Student s = new GradStudent();
int id = s.getID();

The first line is valid, but the second line causes a compile-time error. The variable s is of type Student, and getID is only accessible to GradStudent objects. The error could be fixed by downcasting, which is casting a superclass to a subclass type. The code below would accomplish this.

int id = ((GradStudent) s).getID());

The parenthesis around (GradStudent) s are necessary because the method would be called before the cast otherwise.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram