0% found this document useful (0 votes)
21 views23 pages

Csc435 Chapter 5 Part 2

The document discusses the concept of inheritance in Java, detailing how classes derive from the Object class and the access levels (public, protected, private, package) that control member accessibility. It explains the use of arrays of subclasses, the instanceof operator for checking object types, and the importance of object casting for accessing subclass methods. Additionally, it covers how to override the toString() method in subclasses to include additional information from the superclass.

Uploaded by

2023674672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views23 pages

Csc435 Chapter 5 Part 2

The document discusses the concept of inheritance in Java, detailing how classes derive from the Object class and the access levels (public, protected, private, package) that control member accessibility. It explains the use of arrays of subclasses, the instanceof operator for checking object types, and the importance of object casting for accessing subclass methods. Additionally, it covers how to override the toString() method in subclasses to include additional information from the superclass.

Uploaded by

2023674672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSC435 - OBJECT-ORIENTED

PROGRAMMING
TOPIC 5 PART 2
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
THE OBJECT CLASS
A class called Object is defined in the java.lang package of the Java
standard class library.
All classes are derived from the Object class.

If a class is not explicitly defined to be the child of an existing class, it is


assumed to be the child of the Object class.
Therefore, the Object class is the ultimate root of all class hierarchies.
THE OBJECT CLASS
All objects are guaranteed to have a toString method via inheritance.

Thus the println method can call toString for any object that is passed
to it.
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
MEMBER ACCESSIBILITY
Java has four (4) levels of controlling access to fields, methods, and classes:
public access
 Can be accessed by methods of all classes
protected access
 Can be accessed only by methods that belong to the same class or to the
descendant classes
private access
 Can be accessed only by the methods of their own class
package access
 The default, when no access modifier is given.
 Can be accessed by all classes in the same package.

6
MEMBER ACCESSIBILITY (CONT’D)
The methods of a sub classes objects can access both the public and
protected members of super class.
The methods of a sub classes objects cannot access the private members of
super class.

7
ACCESS MODIFIER

8
ACCESS MODIFIER

Same class
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
ARRAY OF SUBCLASSES
As with arrays of primitive types, arrays of objects allow much more efficient
methods of access.

Note in this example that once the array of Animals has been structured, it can
be used to store objects of any subclass of Animal.
ARRAY OF SUBCLASSES
Superclass: Animal
ARRAY OF SUBCLASSES
Subclass: Cow
ARRAY OF SUBCLASSES
Subclass: Dog
ARRAY OF SUBCLASSES
Subclass: Cat
Application: Animal Application
CHECKING OBJECT TYPE
To check which class an object is created from, we can use instanceof operator.

for (int i=0; i<ref.length; i++) {


if (ref[i] instanceof Cat) {
System.out.println(“this is cat”);
} else if (ref[i] instanceof Cow) {
System.out.println(“this is cow”);
} else {
System.out.println(“this is dog”);
}
}
OBJECT CASTING
We will use object casting when we want to convert object that we store in
superclass data type into the original class data type.
We need to convert the type especially when we want to call
method/processor from the original class which does not exist in superclass.

18
FOR EXAMPLE
We have:
CONT
Animal a[] = new Animal[10];
a[0] = new Frog(“fire-bellied”, “toad”);
a[1] = new Bird(“pigeon”, “white”);
As you can see, even though a[0]
contains object of Frog, we can’t call
a[0].sleep(); //valid the function jump() because the object
is currently stored in Animal data type.
a[1].sleep(); //valid Same with a[1]. Sleep() is valid
a[0].jump(); // not valid because it is declared in Animal, so it
is guaranteed that all classes that
a[1].fly(); // not valid inherit from it will contain the method.

20
CONT
To call the specific method in subclass when the subclass object is stored in superclass
data type, we need to cast the object into subclass data type.

for (int i=0; i<a.length; i++) {


if (a[i] instanceof Frog) {
Frog f = (Frog)a[i];
f.jump(); // valid
}
else if (a[i] instanceof Bird) {
Bird b = (Bird)a[i];
b.fly(); // valid
}
}

21
TOSTRING
For subclasses’ toString() method, we can just call the superclass toString()
method and concat with the new variable inside the subclass. For example,
toString() function for class Frog:

public String toString() {


return super.toString() + “, species=“ + species;
}
The super.toString() will call toString() function in class Animal (which will return
name information) and then we concat it with information in the subclass, which
is species.
REFERENCES

 Deitel, Java How To Program, 5th Edition, Prentice Hall, 2003


 Horstmann, Java Concepts, 4th Edition, John Wiley, 2005
 Malik D.S, Nair P.S. Java Programming: From Problem Analysis To Program Design.
Course Technology 2003.
 Savitch W. Absolute Java. 2nd Edition. Addison Wesley 2006.
 Wu, An Introduction To Object-Oriented Programming With Java, 4th Edition,
McGraw-Hill, 2006

You might also like