Csc435 Chapter 5 Part 1
Csc435 Chapter 5 Part 1
PROGRAMMING
TOPIC 5 PART 1
Inheritance
By Zulaile Mabni
Amended by Mohammad Bakri & Hajar Izzati Mohd Ghazalli
Inheritance concept
Inheriting instance fields and methods
Calling super class constructor and methods
OBJECTIVES Object class
Access levels – public, protected, private, package
Array of sub classes
Application of super and sub classes
INHERITANCE
Define new class (sub class) from existing class (super class).
Extend classes by adding methods and fields in the new class.
Sub class inherits attributes & behavior from super class.
Advantage:
code/software reusability (safe time and cost).
“IS-A” VS. “HAS-A” RELATIONSHIP
“is-a” = Inheritance.
Sub class object treated as super class object.
Super class typically represents larger set of objects than sub classes.
Example: Car “is- a” Vehicle (Car-sub class; Vehicle-super class).
Vehicle properties/behaviors are also car properties/behaviors.
4
“IS-A” VS. “HAS-A” RELATIONSHIP (CONT’D)
5
Inheritance concept
Inheriting instance fields and methods
Calling super class constructor and methods
OBJECTIVES Object class
Access levels – public, protected, private, package
Array of sub classes
Application of super and sub classes
TYPE OF INHERITANCE
Single inheritance
Inherits from one super class.
Multiple inheritance
Inherits from multiple super classes.
7
INHERITANCE EXAMPLES
Super Class Sub Class
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Employee FacultyMember
StaffMember
BankAccount CheckingAccount
SavingsAccount
8
INHERITANCE
Pet
Dog Cat
THE PET CLASS
class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String petName) {
name = petName;
}
public String speak( ) {
return “I’m your cuddly little pet.”;
}
}
SUBCLASSES OF THE PET CLASS
class Cat extends Pet { The Cat subclass
overrides the
public String speak( ) {
inherited method
return “Don’t give me orders.\n” + speak.
“I speak only when I want to.”;
}
}
16
CALLING SUPERCLASS CONSTRUCTOR (1)
public class Pet {
private String name;
public Pet() {
name = “”;
}
}
17
CALLING SUPERCLASS CONSTRUCTOR (2)
public class Pet {
private String name;
public Pet(name) {
this.name = name;
}
}
18
OVERRIDING METHODS
A child class can override the definition of an inherited method in favor of its
own.
The new method must have the same signature as the parent's method, but
can have a different body.
The type of the object executing the method determines which version of the
method is invoked.
OVERRIDING
A parent method can be invoked explicitly using the super reference.
21
OVERLOADING VS. OVERRIDING
22
OVERLOADING VS. OVERRIDING
Don't confuse the concepts of overloading and overriding.
Overloading deals with multiple methods with the same name in the same class,
but with different signatures.
Overriding deals with two methods, one in a parent class and one in a child
class, that have the same signature.
Overloading lets you define a similar operation in different ways for different
data.
Overriding lets you define a similar operation in different ways for different
object types.