Lecture4
Lecture4
Fall 2020/21
Object-Oriented Programming
CS-201, CS201, C212
Lec. (4)
Inheritance and Polymorphism
Inheritance and Polymorphism
• After you have read and studied this chapter, you should be able to
• Write programs that are easily extensible and modifiable by applying
polymorphism in program design.
• Define reusable classes based on inheritance and abstract classes and
abstract methods.
• Differentiate the abstract classes and Java interfaces.
• Define methods, using the protected modifier.
• Parse strings, using a String Tokenizer object.
Defining Classes with Inheritance
«Sub '
«Sub Class» Class»
Student Employei!
· Salary
· Study_Level · Rank
· Specialization
- Job
- GPA
+Set_Study_level ( ) + Set_Salary()
+Get_Study_level ( ) + Get_Salary ( )
+Set.Specialization () + Set.rank ()
+Get_Specialization ( ) + Get.rank ( )
+Set_GPA( I + Set.Job ()
+Get_GPA( ) + Get)ob ()
t
I
«Sub Class»
I
«Sub Class»
Salaried Employei! Hour1y Employee
· Bonus ·Working_Hours
• Deductions · Houre_rate
+Set_Worl<ing_hours ( ) +Set_Worl<ing_hours ( )
+ Get_Working_hours ( ) +Get_Working_hours( )
+ Set_Houre_Rate ( I + Set_Houre_Rate ( I
+ Get_Houre_Rate ( ) +Get_Houre_Rate ( )
The “is a” Relationship
• The relationship between a superclass and an inherited class is called an
“is a” relationship.
– A post graduate student “is a” Student.
– An Employee “is a” Person.
– Salaried Employee “is a” Employee.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used to create an “is a”
relationship among classes.
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent classes and child classes.
Inheritance
• The subclass inherits fields and methods from the superclass without any of
them being rewritten.
• New fields and methods may be added to the subclass.
• The Java keyword, extends, is used on the class header to define the subclass.
• An object of the subclass invokes the subclass’s version of the method, not the superclass’s.
• The @Override annotation should be used just before the subclass method declaration.
Employee
public double get_salary( )
{
return salary;
}
Salaried Employee
public double get_salary( )
{
return salary + bonus - deductions ;
}
Hourly Employee
public double get_salary( )
{
return working_hours * hours_rate ;
}
Inheritance and Member Accessibility
• We use the following visual representation of
inheritance to illustrate data member accessibility.
Instances
Class Hierarchy
The Effect of Three Visibility Modifiers
Accessibility of Super from Sub
• Everything except the private members of the Super class is
visible from a method of the Sub class.
Accessibility from Another Instance
• Data members accessible from an instance are also accessible
from other instances of the same class.
Inheritance and Constructors
• Unlike members of a superclass, constructors of a
superclass are not inherited by its subclasses.
• You must define a constructor for a class or use the
default constructor added by the compiler.
• The statement
super();
calls the superclass’s constructor.
• If the class declaration does not explicitly designate the
superclass with the extends clause, then the class’s
superclass is the Object class.
Abstract Superclasses and Abstract Methods