[course06]01 Inheritance

[course06] 01 Inheritance

Superclass and subclass

Inheritance defines a relationship between objects that share characteristics.

Specifically it is the mechanism whereby a new class, called a subclass, is created from an existing class, called a superclass, by absorbing its state and behavior and augmenting these with features unique to the new class. We say that the subclass inherits characteristics of its superclass.

Inheritance Hierarchy

For any of these classes, an arrow points to its superclass. The arrow designates an inheritance relationship between classes, or, informally, an is-a relationship.

Note that the is-a relationship is transitive: If a GradStudent is-a Student and a Student is-a Person, then a GradStudent is-a Person.

  • Every subclass inherits the public or protected variables and methods of its superclass

  • Subclasses may have additional methods and instance variables that are not in the superclass.

  • A subclass may redefine a method it inherits.

Implementing Subclasses

example class student

Student.java

UnderGrad.java

GradStudent.java

Inheriting instance methods and variables

A subclass inherits all the public and protected methods of its superclass.

It does not, however, inherit the private instance variables or private methods of its parent class, and therefore does not have direct access to them. To access private instance variables, a subclass must use the accessor or mutator methods that it has inherited.

Method overriding and the super keyword

Any public method in a superclass can be overridden in a subclass by defining a method with the same return type and signature (name and parameter types).

Sometimes the code for overriding a method includes a call to the superclass method. This is called partial overriding. Typically this occurs when the subclass method wants to do what the superclass does, plus something extra.

Constructors and super

Constructors are never inherited! If no constructor is written for a subclass, the superclass default constructor with no parameters is generated. If the superclass does not have a default (zeroparameter) constructor, but only a constructor with parameters, a compiler error will occur. If there is a default constructor in the superclass, inherited data members will be initialized as for the superclass. Additional instance variables in the subclass will get a default initialization—0 for primitive types and nXll for reference types.

A subclass constructor can be implemented with a call to the sXper method, which invokes the superclass constructor.

Declaring Subclass Objects

Note that since a Student is not necessarily a GradStudent nor an UnderGrad, the following declarations are not valid:

Consider these valid declarations:

Polymorphism

A method that has been overridden in at least one subclass is said to be polymorphic.

Polymorphism is the mechanism of selecting the appropriate method for a particular object in a class hierarchy. The correct method is chosen because, in Java, method calls are always determined by the type of the actual object, not the type of the object reference.

Dynamic Binding (Late Binding)

Making a run-time decision about which instance method to call is known as dynamic binding or late binding. Contrast this with selecting the correct method when methods are overloaded rather than overridden. The compiler selects the correct overloaded method at compile time by comparing the methods’ signatures. This is known as static binding, or early binding. In polymorphism, the actual method that will be called is not determined by the compiler.

Polymorphism applies only to overridden methods in subclasses.

Using super in a Subclass

A subclass can call a method in its superclass by using super. Suppose that the superclass method then calls another method that has been overridden in the subclass. By polymorphism, the method that is executed is the one in the subclass. The computer keeps track and executes any pending statements in either method.

Type Compatibility

Downcasting

At compile time, only nonprivate methods of the Student class can appear to the right of the dot operator when applied to s. Don’t confuse this with polymorphism: getID is not a polymorphic method. It occurs in just the GradStudent class and can therefore be called only by a GradStudent object.

  1. The outer parentheses are necessary, so

will still cause an error, despite the cast. This is because the dot operator has higher precedence than casting, so s.getID() is invoked before s is cast to GradStudent.

  1. The statement

compiles without problem because g is declared to be of type GradStXdent, and this is the class that contains getID. No cast is required.

  1. Class casts will not explicitly be tested on the AP exam. You should, however, understand why the following statement will cause a compile-time error:

And the following statement will compile without error:

Last updated

Was this helpful?