0% found this document useful (0 votes)
9 views

Lecture4

The document discusses Object-Oriented Programming concepts, focusing on inheritance and polymorphism. It explains how to create reusable classes using inheritance, differentiates between abstract classes and interfaces, and illustrates the 'is a' relationship in class hierarchies. Additionally, it covers method overriding, member accessibility, and the use of the protected modifier in class design.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture4

The document discusses Object-Oriented Programming concepts, focusing on inheritance and polymorphism. It explains how to create reusable classes using inheritance, differentiates between abstract classes and interfaces, and illustrates the 'is a' relationship in class hierarchies. Additionally, it covers method overriding, member accessibility, and the use of the protected modifier in class design.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Faculty of Information Technology

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

• Real-life objects are typically specialized versions of other more


general objects
• Case Study:
• Suppose we want implement a class roster that contains both undergraduate
and graduate students.
• Each student’s record will contain his or her name, three test scores, and the
final course grade.
• The formula for determining the course grade is different for graduate
students than for undergraduate students.
Modeling Two Types of Students
• There are two ways to design the classes to model
undergraduate and graduate students.
• We can define two unrelated classes, one for undergraduates and one for
graduates.
• We can model the two kinds of students by using classes that are related in an
inheritance hierarchy.
• Two classes are unrelated if they are not connected in an
inheritance relationship.
Classes for the Class Roster

• For the Class Roster sample, we design three classes:


• Student
• UndergraduateStudent
• GraduateStudent
• The Student class will incorporate behavior and data common to both
UndergraduateStudent and GraduateStudent objects.
• The UndergraduateStudent class and the GraduateStudent class will each
contain behaviors and data specific to their respective objects.
Inheritance Hierarchy
Without Inheritance
«Super Class»
Person
· Name
·Age
·Address
• Nationality
+Set_Age ( )
+Get_Age ()
+Set_address ( )
+Get_address ( )
+Set_Nationality ()
+Get_Nationality (I

«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.

public class Employee extends Person


Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods of the superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
Inheritance and Constructors
• Constructors are not inherited.
• When a subclass is instantiated, the superclass default constructor is
executed first.
• The super keyword refers to an object’s superclass.
• The superclass constructor can be explicitly called from the subclass by
using the super keyword.
Overriding Superclass Methods
• A subclass may have a method with the same signature as a superclass method.

• The subclass method overrides the superclass method.

• This is known as method overriding.


• A subclass method that overrides a superclass method must have the same signature as the
superclass method.

• 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

This shows the inherited


components of the
superclass are part of
the subclass instance

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

• When we define a superclass, we often do not need to create any


instances of the superclass.
• Depending on whether we need to create instances of the superclass,
we must define the class differently.
• We will study examples based on the Student superclass defined
earlier.
Definition: Abstract Class
• An abstract class is a class
• defined with the modifier abstract OR
• that contains an abstract method OR
• that does not provide an implementation of an inherited abstract method

• An abstract method is a method with the keyword abstract, and


it ends with a semicolon instead of a method body.
• Private methods and static methods may not be declared abstract.

• No instances can be created from an abstract class.


Which Approach to Use

• The best approach depends on the particular situation.

• When considering design options, we can ask ourselves which


approach allows easier modification and extension.
Inheritance versus Interface
• The Java interface is used to share common behavior (only
method headers) among the instances of different classes.
• Inheritance is used to share common code (including both
data members and methods) among the instances of related
classes.
• In your program designs, remember to use the Java interface
to share common behavior. Use inheritance to share common
code.
• If an entity A is a specialized form of another entity B, then
model them by using inheritance. Declare A as a subclass of
B.
The Protected Modifier

• The modifier protected makes a data member or method visible and


accessible to the instances of the class and the descendant classes.

• Public data members and methods are accessible to everyone.

• Private data members and methods are accessible only to instances


of the class.

You might also like