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

Csc435 Chapter 5 Part 1

The document discusses the concept of inheritance in object-oriented programming, highlighting the relationship between super classes and sub classes, and the advantages of code reusability. It explains the differences between 'is-a' and 'has-a' relationships, types of inheritance, and provides examples of class hierarchies. Additionally, it covers the use of constructors, method overriding, and the distinction between overloading and overriding methods.

Uploaded by

2023674672
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Csc435 Chapter 5 Part 1

The document discusses the concept of inheritance in object-oriented programming, highlighting the relationship between super classes and sub classes, and the advantages of code reusability. It explains the differences between 'is-a' and 'has-a' relationships, types of inheritance, and provides examples of class hierarchies. Additionally, it covers the use of constructors, method overriding, and the distinction between overloading and overriding methods.

Uploaded by

2023674672
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSC435 - OBJECT-ORIENTED

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)

“has-a” = Composition / nested.


Object contains one or more objects of other classes as members.
Example: Car “has- a” steering wheel .

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.

Java only permits single inheritance.

7
INHERITANCE EXAMPLES
Super Class Sub Class

Student GraduateStudent
UndergraduateStudent

Shape Circle
Triangle
Rectangle

Employee FacultyMember
StaffMember

BankAccount CheckingAccount
SavingsAccount

8
INHERITANCE

Inheritance relationships often are shown


graphically in a UML class diagram, with an
arrow with an open arrowhead pointing to
the parent class.
Inheritance should create an is-a
relationship, meaning the child is a more
specific version of the parent.
DERIVING SUBCLASSES
In Java, we use the reserved word extends to establish an inheritance
relationship.

class Car extends Vehicle


{
// class contents
}
ANOTHER EXAMPLE
Suppose we want to model dogs and cats. They are different types of pets. We can
define the Pet class and the Dog and Cat classes as the subclasses of the Pet class.

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.”;
}
}

class Dog extends Pet { The Dog subclass


public String fetch( ) { adds a new method
fetch.
return “Yes, master. Fetch I will.”;
}
}
SAMPLE USAGE OF THE SUBCLASSES
Dog myDog = new Dog();
I’m your cuddly little pet.
Yes, master. Fetch I will.
System.out.println(myDog.speak());
System.out.println(myDog.fetch());

Cat myCat = new Cat();


Don’t give me orders.
I speak only when I want to.
System.out.println(myCat.speak());
System.out.println(myCat.fetch()); ERROR
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 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.
A constructor of the base class can be invoked within the definition of a
derived class constructor.
Any invocation of super() must be the first action taken by the constructor.
If the class declaration does not explicitly designate the superclass with the
extends clause, then the class’s superclass is the Object class.

16
CALLING SUPERCLASS CONSTRUCTOR (1)
public class Pet {
private String name;
public Pet() {
name = “”;
}
}

public class Cat extends Pet {


private String breed;
public Cat() {
super(); // calling superclass default constructor
breed = “”;
}
}

17
CALLING SUPERCLASS CONSTRUCTOR (2)
public class Pet {
private String name;
public Pet(name) {
this.name = name;
}
}

public class Cat extends Pet {


private String breed;
public Cat(name, breed) {
super(name); // calling superclass normal constructor
this.breed = breed;
}
}

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.

If a method is declared with the final modifier, it cannot be overridden.


OVERRIDING EXAMPLE
public class Pet {
public void jump() {
System.out.println(“jump”);
} //Our main program…
public void speak() { Cat cat1 = new Cat();
System.out.println(“nothing”);
//output jump
}
} cat1.jump();
// output meow
cat1.speak();
public class Cat extends Pet {
public void speak() {
System.out.println(“meow”);
}
}

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.

You might also like