Inheritance and Polymorphism

Inheritance

"is-a" relationship

Single inheritance

subclass is derived from one existing class (superclass)

Multiple inheritance

subclass is derived from more than one superclass

not supported by Java

In Java, a class can only extend the definition of one class

Inheritance: class Circle derived from class Shape

public class Circle extends Shape { ... }

Rules

private members of the superclass are private to the superclass

subclass can directly access the public members of the superclass

subclass can include additional data and/or method members

subclass can override or redefine the public methods of the superclass (applies only to the objects of the subclass)

all data members of the superclass are also data members of the subclass

methods of the superclass (unless overridden) are also methods of the subclass

Defining Constructors of the Subclass

Call to constructor of superclass

must be first statement

specified by: super parameter list

specified in the definition of a constructor of the subclass

Shadowing variables

the concept of a shadowing variable is similar to the concept of overriding a method, but it causes confusion

suppose that the class subClass is derived from the class SuperClass and SuperClass has a variable named temp

you can declare a variable temp in the class SubClass; in this case, the variable temp of SubClass is called a shadowing variable

the SubClass is derived from SuperClass, so it inherits the variable temp of SuperClass

Because a variable named temp is already available in SubClass, there is seldom if ever any reason to override it

Protected Members of a Class

private to the class and cannot be directly accessed outside the class

only methods of that class can access directly

make a private member public for anyone to access that member

a subclass can directly access the protected member of a superclass

Protected Access vs. Package Access

if a class member is declared without any of the modifiers public, private, or public, then the Java system gives to that member the default package access (that member can be directly accessed in any class contained in that package)

that member can be directly accessed in any class contained in that package

if a member of a class is protected, that member can be directly accessed in any subclass even if the subclass is contained in a different package

The class Object

directly or indirectly becomes the superclass of every class in Java

public members of class Object can be overridden/invoked by object of any class type

Polymorphism

Java allows us to treat an object of a subclass as an object of its superclass

a reference variable of a superclass type can point to an object of its subclass

Late binding or dynamic binding (run-time binding)

method executed determined at execution time, not compile time

assigning multiple meanings to the same method name

implemented using late binding

polymorphic reference variables

have many forms

they can refer to objects of their own class or objects of the classes inherited from their class

declare a method of a class final using the keyword final

it cannot be overridden with a new definition in a derived class

declare a class final using the keyword final

no other class can be derived from this class

not applicable for private, final, and static methods

you cannot automatically make reference variable of subclass type point to object of its superclass

Operator instanceof

determines whether reference variable that points to object is of particular class type

Example: p instanceof BoxShape

this expression evaluates to true if p points to an object of the class BoxShape; otherwise it evaluates to false

Abstract Methods

method that has only the heading with no body

must be declared abstract

Abstract Class

a class that is declared with the reserved word abstract in its heading

can contain instance variables, constructors, finalizer, and non-abstract methods

can contain abstract method(s)

you cannot instantiate an object of an abstract class type; you can only declare a reference variable of an abstract class type

you can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass

Interfaces

a class that contains only abstract methods and/or named constants

Java allows a class to implement more than one interface to be able to handle a variety of events

can be used in the implementation of abstract data types

Composition (Aggregation)

another way to relate two classes

one or more members of a class are objects of another class type

"has-a" relation between classes