Lec05 Inheritance, Comp and Polymorphism
Lec05 Inheritance, Comp and Polymorphism
Java
6
Inheritance: class Circle Derived
from class Shape
public class Circle extends Shape
{
.
.
.
}
7
Inheritance Rules
1. The private members of the superclass are
private to the superclass
2. The subclass can directly access the public
members of the superclass
3. The subclass can include additional data and/or
method members
8
Inheritance Rules (continued)
4. The subclass can override, that is, redefine
the public methods of the superclass
– However, this redefinition applies only to the
objects of the subclass, not to the objects of the
superclass
5. All data members of the superclass are also
data members of the subclass
– Similarly, the methods of the superclass (unless
overridden) are also the methods of the subclass
– Remember Rule 1 when accessing a member of
the superclass in the subclass
9
Inheritance (continued)
• To write a method’s definition of a subclass,
specify a call to the public method of the
superclass
– If subclass overrides public method of superclass,
specify call to public method of superclass:
super.MethodName(parameter list)
– If subclass does not override public method of
superclass, specify call to public method of
superclass:
MethodName(parameter list)
10
UML Class Diagram: class
Rectangle
11
UML Class Diagram: class
Box
12
class Box
public String toString()
{
return super.toString() //retrieve length and width
+ " Height = " + height;
}
public void setDimension(double l, double w, double h)
{
super.setDimension(l, w);
if (h >= 0)
height = h;
else
height = 0;
}
public double area()
{
return 2 * (getLength() * getWidth()
+ getLength() * height
+ getWidth() * height);
}
13
Defining Constructors of the Subclass
• Call to constructor of superclass
– Must be first statement
– Specified by: super parameter list
public Box()
{
super();
height = 0;
}
14
Objects myRectangle and myBox
Rectangle myRectangle = new Rectangle(5, 3);
Box myBox = new Box(6, 5, 4);
15
Constructors
• A call to a constructor of a superclass is specified in the
definition of a constructor of the subclass
• When a subclass constructor executes, first a constructor
of the superclass executes to initialize the data members
inherited from the superclass and then the constructor of
the subclass executes to initialize the data members
declared by the subclass
• First the constructor of the class Rectangle executes to
initialize the instance variables length and width and then
the constructor of the class Box executes to initialize the
instance variable height
16
Shadowing Variables
• Suppose that the class SubClass is derived from the class
SuperClass and SuperClass has a variable named temp
• You can declare a variable named temp in the class
SubClass; in this case, the variable temp of SubClass is
called a shadowing variable
• The concept of a shadowing variable is similar to the concept of
overriding a method, but it causes confusion
• 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
• It is poor programming practice to override a variable in the
SubClass
17
Shadowing Variables (continued)
• Anyone reading code with a shadowed variable will have two
different declarations of a variable seeming to apply to the
shadowed variable of the SubClass
• This causes confusion and should be avoided; in general, you
should avoid shadowing variables
18
UML Class Diagram
19
Protected Members of a Class
• The private members of a class are private to the class and
cannot be directly accessed outside the class
• Only methods of that class can access the private members
directly; as discussed previously, the subclass cannot access the
private members of the superclass directly
• If you make a private member public, then anyone can
access that member
• If a member of a superclass needs to be (directly) accessed in a
subclass and yet still prevent its direct access outside the class,
you must declare that member using the modifier protected
• The accessibility of a protected member of a class falls
between public and private
• A subclass can directly access the protected member of a
superclass
20
Protected Access vs. Package Access
• Typically a member of a class is declared with the
modifier public, private, or protected to give appropriate
access to that member
• You can also declare a member without any of these
modifiers
• 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 is, that
member can be directly accessed in any class contained in
that package
• There is a subtle difference between package access and
protected accesses of a member
21
Protected Access vs. Package Access
(continued)
• If a member of a class has a package access, that member
can be directly accessed in any class contained in that
package, but not in any class that is not contained in that
package even if a subclass is derived from the class
containing that member and the subclass is not contained
in that package
• On the other hand, 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
22
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
23
The class Object:
Equivalent Definition of a Class
public class Clock
{
//Declare instance variables as given in Chapter 8
//Definition of instance methods as given in Chapter 8
//...
}
24
Some Constructors and Methods
of the class Object
25
Hierarchy of Java Stream Classes
26
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
– e.g., “every person has a date of birth”
30
Composition (Aggregation) Example
31
Composition (Aggregation) Example
(continued)
32
Polymorphism
• Java allows us to treat an object of a subclass as an
object of its superclass
– In other words, a reference variable of a superclass type
can point to an object of its subclass
Person name, nameRef;
PartTimeEmployee employee, employeeRef;
name = new Person("John", "Blair");
employee = new PartTimeEmployee("Susan", "Johnson",
12.50, 45);
nameRef = employee;
System.out.println("nameRef: " + nameRef);
nameRef: Susan Johnson wages are: $562.5
37
Polymorphism (continued)
• Late binding or dynamic binding (run-time
binding)
– Method executed determined at execution time, not
compile time
• The term polymorphism means assigning
multiple meanings to the same method name
• In Java, polymorphism is implemented using late
binding
• The reference variable name or nameRef can
point to any object of the class Person or
the class PartTimeEmployee
38
Polymorphism (continued)
• These reference variables have many forms;
that is, they are polymorphic reference
variables
– They can refer to objects of their own class or
objects of the classes inherited from their class
• You can declare a method of a class final
using the key word final; for example,
the following method is final:
public final void doSomeThing()
{
//...
}
39
Polymorphism (continued)
• If a method of a class is declared final, it
cannot be overridden with a new definition in a
derived class
• In a similar manner, you can also declare a
class final using the keyword final
• If a class is declared final, then no other
class can be derived from this class
40
Polymorphism (continued)
• You cannot automatically make reference
variable of subclass type point to object of its
superclass
• Suppose that supRef is a reference variable of a
superclass type; moreover, suppose that supRef
points to an object of its subclass
• You can use an appropriate cast operator on
supRef and make a reference variable of the
subclass point to the object
41
Polymorphism (continued)
• On the other hand, if supRef does not
point to a subclass object and you use a cast
operator on supRef to make a reference
variable of the subclass point to the object,
then Java will throw a
ClassCastException, indicating that
the class cast is not allowed
42
example
43
Polymorphism (continued)
44
Abstract Methods
• Abstract method: method that has only the
heading with no body
– Must be declared abstract
public AbstractClassExample()
{
x = 0;
}
}
Java Programming: From Problem
47
Analysis to Program Design, 5e
Interfaces
• Definition: class that contains only abstract
methods and/or named constants
• How Java implements multiple inheritance
• To be able to handle a variety of events,
Java allows a class to implement more than
one interface
51
Components Course and Student
52
Components Course and Student (continued)
53
Programming Example: Grade
Report
• Operations on student
– Set student information
– Print student information
– Calculate number of credit hours taken
– Calculate GPA
– Calculate billing amount
– Sort the courses according to the course number
54
Programming Example: Grade
Report (continued)
• Main algorithm
– Declare variables
– Open input file
– Open output file
– Get number of students registered and tuition
rate
– Load students’ data
– Print grade reports
55
Chapter Summary
• Inheritance
– Single and multiple
– Rules
– Uses
– Superclasses/subclasses (objects)
– Overriding/overloading methods
– Constructors
• The class Object
56
Chapter Summary (continued)
• Inheritance
• Polymorphism
• Composition
57