0% found this document useful (0 votes)
2 views15 pages

Module 06 Inheritance

This instructional module from Nueva Vizcaya State University focuses on the concept of inheritance in Java programming, detailing its significance in object-oriented programming. It covers key topics such as subclass and superclass relationships, method overriding, and various types of inheritance, including single, multilevel, hierarchical, and the limitations of multiple inheritance in Java. The module also provides examples and desired learning outcomes for students to understand and apply the principles of inheritance effectively.

Uploaded by

rickhunnob14
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)
2 views15 pages

Module 06 Inheritance

This instructional module from Nueva Vizcaya State University focuses on the concept of inheritance in Java programming, detailing its significance in object-oriented programming. It covers key topics such as subclass and superclass relationships, method overriding, and various types of inheritance, including single, multilevel, hierarchical, and the limitations of multiple inheritance in Java. The module also provides examples and desired learning outcomes for students to understand and apply the principles of inheritance effectively.

Uploaded by

rickhunnob14
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

Republic of the Philippines

NUEVA VIZCAYA STATE UNIVERSITY


Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

College : College of Engineering


Campus : Bambang Campus

DEGREE PROGRAM BSCPE COURSE NO. CPE3


SPECIALIZATION Java Programming COURSE TITLE Object – Oriented Programming
YEAR LEVEL 1st Year TIME FRAME 6hrs WK NO. 13 IM NO. 06
I. UNIT TITLE/CHAPTER TITLE
Inheritance
II. LESSON TITLE
1. What is inheritance?
2. Subclass and superclass?
3. Method overriding in inheritance.
III. LESSON OVERVIEW
Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent
object. It is an important part of OOPs (Object Oriented programming system).
IV. DESIRED LEARNING OUTCOMES
After covering the topics from this chapter, students are able to;
1. Define what is inheritance?
2. Apply the concept of inheritance in OOP.
3. Determine the different types of inheritance.
4. Learn how to override methods in inheritance.
V. LESSON CONTENT
1. What is inheritance? Inheritance is one of the key features of OOP that allows us to create a new
class from an existing class. Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).

Customer Class inherits everything from Person Class


Why use inheritance in java
a. For Method Overriding (so runtime polymorphism can be achieved).
b. For Code Reusability.
Inheritance is “is-a” relationship In Java, inheritance is an is-a relationship. That is, we use
inheritance only if there exists an is-a relationship between two classes. For example,
a. Car is a Vehicle
b. Orange is a Fruit
c. Surgeon is a Doctor
d. Dog is an Animal
Here, Car can inherit from Vehicle, Orange can inherit from Fruit, and so on.

NVSU-FR-ICD-05-00 (081220) Page 1 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Terms used in Inheritance
a. Class: A class is a group of objects which have common properties. It is a template or blueprint
from which objects are created.
b. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a
derived class, extended class, or child class.
c. Super Class/Parent Class: Superclass is the class from where a subclass inherits the features.
It is also called a base class or a parent class.
d. Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the
fields and methods of the existing class when you create a new class. You can use the same
fields and methods already defined in the previous class.
Types of Inheritance
a. Single Inheritance - When a class inherits from a single class.

CLASS A

CLASS B

b. Multilevel Inheritance - When there is a chain of inheritance.

CLASS A

CLASS B

CLASS C

c. Hierarchical Inheritance - When two or more classes inherits a single class.

CLASS A

CLASS B CLASS B

d. Multiple Inheritance - When one class inherits multiple classes. (not supported by java)

CLASS A CLASS B

CLASS C

e. Hybrid Inheritance – a combination of 2 or more of the above types of inheritance. (not supported
by java)
CLASS A

CLASS A CLASS B

CLASS C

NVSU-FR-ICD-05-00 (081220) Page 2 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
2. How to apply inheritance in java. The new class that is created is known as subclass (child or
derived class) and the existing class from where the child class is derived is known as superclass
(parent or base class).
The extends keyword is used to perform inheritance in Java. For example,
class Animal
{
// methods and fields of class Animal
}

// use of extends keyword to perform inheritance


class Dog extends Animal
{

// inherits methods and fields of class Animal


// you can add methods and fields of class Dog
}

In the above example, the Dog class is created by inheriting the methods and fields from the Animal
class.
Here, Dog is the subclass and Animal is the superclass.
Example 1: Single Inheritance
class Animal
{
// field and method of the parent class
String name;
public void eat()
{
[Link]("I can eat");
}
}
// inherit from Animal
class Dog extends Animal
{
// new method in subclass
public void display()
{
[Link]("My name is " + name);
}
}
public class main
{
public static void main(String[] args)
{
// create an object of the subclass
Dog labrador = new Dog();
// access field of superclass
[Link] = "Rohu";
[Link]();
// call method of superclass using object of subclass
[Link]();
}
}
Here, labrador is an object of Dog. However, name and eat() are the members of the Animal class.
Since Dog inherits the field and method from Animal, we are able to access the field and method
using the object of the Dog.
Output Example 1
My name is Rohu
I can eat

NVSU-FR-ICD-05-00 (081220) Page 3 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

Example 2: Multilevel Inheritance


class Animal
{
void eat()
{
[Link]("eating...");
}
}

class Dog extends Animal


{
//has everything from Animal Class
void bark()
{
[Link]("barking...");
}
}
class BabyDog extends Dog
{
//has everything from Dog Class including those
//that were inherited by the Dog Class from Animal Class
void weep()
{
[Link]("weeping...");
}
}

public class main


{
public static void main(String[] args)
{
BabyDog d=new BabyDog();
[Link]();
[Link]();
[Link]();
}
}
As you can see in the example given below, BabyDog class inherits the Dog class which again inherits
the Animal class, so there is a multilevel inheritance.

Output Example 2
weeping...
barking...
eating...

NVSU-FR-ICD-05-00 (081220) Page 4 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Example 3: Hierarchical Inheritance
class Animal
{
void eat()
{
[Link]("eating...");
}
}

class Dog extends Animal


{
//has everything from Animal Class
void bark()
{
[Link]("barking...");
}
}

class Cat extends Animal


{
//has everything from Animal Class
void meow()
{
[Link]("meowing...");
}
}

public class main


{
public static void main(String[] args)
{
Cat c = new Cat();
[Link]();
[Link]();
}
}

In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical
inheritance.

Output Example 3
meowing...
eating...

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A
and B classes have the same method and you call it from child class object, there will be ambiguity
to call the method of A or B class. Since compile-time errors are better than runtime errors, Java
renders compile-time error if you inherit 2 classes. So whether you have same method or different,
there will be compile time error.

But we can use Interfaces to implement multiple inheritance

3. Method overriding in java inheritance

In Example 1, we see the object of the subclass can access the method of the superclass.

However, if the same method is present in both the superclass and subclass, what will happen?

In this case, the method in the subclass overrides the method in the superclass. This concept is
known as method overriding in Java.

NVSU-FR-ICD-05-00 (081220) Page 5 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Example 4: Overriding
class Animal
{
// method in the superclass
public void eat()
{
[Link]("I can eat");
}
}

// Dog inherits Animal


class Dog extends Animal
{

// overriding the eat() method


@Override
public void eat()
{
[Link]("I eat dog food");
}

// new method in subclass


public void bark() {
[Link]("I can bark");
}
}

public class main


{
public static void main(String[] args)
{
// create an object of the subclass
Dog labrador = new Dog();

// call the eat() method


[Link]();
[Link]();
}
}

Output Example 4
I eat dog food
I can bark

In the above example, the eat() method is present in both the superclass Animal and the subclass
Dog.

Here, we have created an object labrador of Dog.

Now when we call eat() using the object labrador, the method inside Dog is called. This is because
the method inside the derived(sub) class overrides the method inside the base(super) class. This is
called method overriding.
.
Note: We have used the @Override annotation to tell the compiler that we are overriding a method.
However, the annotation is not mandatory.

NVSU-FR-ICD-05-00 (081220) Page 6 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Example 5: Overriding
class Animal
{
public void displayInfo()
{
[Link]("I am an animal.");
}
}

class Dog extends Animal


{
// has everything from animal class but overrides the displayInfo() method
@Override
public void displayInfo()
{
[Link]("I am a dog.");
}
}
public class Main
{
public static void main(String[] args)
{
Dog d1 = new Dog();
[Link]();
}
}

Output Example 5
I am a dog.

In the above program, the displayInfo() method is present in both the Animal superclass and the Dog
subclass.

When we call displayInfo() using the d1 object (object of the subclass), the method inside the subclass
Dog is called. The displayInfo() method of the subclass overrides the same method of the superclass.

Java Overriding Rules


a. Both the superclass and the subclass must have the same method name, the same return type
and the same parameter list.
b. We cannot override the method declared as final and static.
c. We should always override abstract methods of the superclass.

4. “super” keyword in java inheritance

A common question that arises while performing overriding in Java is:

Can we access the method of the superclass after overriding?

Well, the answer is Yes. To access the method of the superclass from the subclass, we use the super
keyword.

Uses of super keyword


a. To call methods of the superclass that is overridden in the subclass.
b. To access attributes (fields) of the superclass if both superclass and subclass have attributes with
the same name.
c. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass
constructor.

NVSU-FR-ICD-05-00 (081220) Page 7 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Example 6: using ‘super’ to Call Superclass Method
class Animal
{

// overridden method
public void displayInfo()
{
[Link]("I am an animal.");
}

class Dog extends Animal


{

// overriding method: has everything from the Animal class


@Override
public void displayInfo()
{
// call displayInfo() of the superclass.
[Link]();

[Link]("I am a dog.");
}

public class Main


{

public static void main(String[] args)


{
//create an object d1 form Dog Class
Dog d1 = new Dog();

//call the displayInfo() method of Dog Class


[Link]();
}

Output Example 6
I am an animal.
I am a dog.

In the above example, the subclass Dog overrides the method displayInfo() of the superclass Animal.

When we call the method displayInfo() using the d1 object of the Dog subclass, the method inside
the Dog subclass is called; the method inside the superclass is not called.

Inside displayInfo() of the Dog subclass, we have used [Link]() to call displayInfo() of the
superclass.
It is important to note that constructors in Java are not inherited. Hence, there is no such thing as
constructor overriding in Java. However, we can call the constructor of the superclass from its
subclasses. For that, we use super().

NVSU-FR-ICD-05-00 (081220) Page 8 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

Example 7: using ‘super’ to Call Superclass Method


class Animal
{

// overridden method
public void display()
{
[Link]("I am an animal");
}

class Dog extends Animal


{

// overriding method: has everything from the Animal class


@Override
public void display()
{
[Link]("I am a dog");
}

public void printMessage()


{
// this calls overriding method
display();

// this calls overridden method


[Link]();
}

public class Main


{

public static void main(String[] args)


{
//create a dog1 object from Dog class
Dog dog1 = new Dog();

//call printMessage() from Dog class


[Link]();
}

Output Example 7
I am a dog
I am an animal

NVSU-FR-ICD-05-00 (081220) Page 9 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
5. Access Attributes of the Superclass

Example 8: Accessing Attributes of the Superclass


class Animal
{
protected String type = "animal";
}

class Dog extends Animal


{
public String type = "mammal";

public void printType()


{
[Link]("I am a " + type);
[Link]("I am an " + [Link]);
}
}

public class Main


{
public static void main(String[] args)
{
Dog dog1 = new Dog();
[Link]();
}
}

Output Example 8
I am a mammal
I am an animal

In this example, we have defined the same instance field type in both the superclass Animal and the
subclass Dog.

We then created an object dog1 of the Dog class. Then, the printType() method is called using this
object.

Inside the printType() function,


a. type refers to the attribute of the subclass Dog.
b. [Link] refers to the attribute of the superclass Animal.

Hence, [Link]("I am a " + type); prints “I am a mammal”. And, [Link]("I am an


" + [Link]); prints “I am an animal”.

6. Use of super() to access superclass constructor

As we know, when an object of a class is created, its default constructor is automatically called.

To explicitly call the superclass constructor from the subclass constructor, we use super(). It's a
special form of the super keyword.

super() can be used only inside the subclass constructor and must be the first statement.

NVSU-FR-ICD-05-00 (081220) Page 10 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025
Example 9: The use of super()
class Animal
{
// default or no-arg constructor of class Animal
Animal()
{
[Link]("I am an animal");
}
}

class Dog extends Animal


{

// default or no-arg constructor of class Dog


Dog()
{

// calling default constructor of the superclass


super();

[Link]("I am a dog");
}
}

public class Main


{
public static void main(String[] args)
{
Dog dog1 = new Dog();
}

Output Example 9
I am an animal
I am a dog

Here, when an object dog1 of Dog class is created, it automatically calls the default or no-arg
constructor of that class.

Inside the subclass constructor, the super() statement calls the constructor of the superclass and
executes the statements inside it. Hence, we get the output “I am an animal”.

The flow of the program then returns back to the subclass constructor and executes the remaining
statements. Thus, “I am a dog” will be printed.

However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the
compiler implicitly calls the default constructor of the superclass.

So, why use redundant code if the compiler automatically invokes super()?

It is required if the parameterized constructor (a constructor that takes arguments) of the superclass
has to be called from the subclass constructor.

The parameterized super() must always be the first statement in the body of the constructor of the
subclass, otherwise, we get a compilation error.

NVSU-FR-ICD-05-00 (081220) Page 11 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

7. Call Parameterized Constructor Using super()

Example 10: The use of super()


class Animal
{
// default or no-arg constructor
Animal()
{
[Link]("I am an animal");
}

// parameterized constructor
Animal(String type)
{
[Link]("Type: " + type);
}
}

class Dog extends Animal


{
// default constructor
Dog()
{
// calling parameterized constructor of the superclass
super("Animal");

[Link]("I am a dog");
}
}

public class Main


{
public static void main(String[] args)
{
Dog dog1 = new Dog();
}
}

Output Example 10
Type: Animal
I am a dog

The compiler can automatically call the no-argument constructor. However, it cannot call
parameterized constructors.

If a parameterized constructor has to be called, we need to explicitly define it in the subclass


constructor.

Note that in the above example, we explicitly called the parameterized constructor super("Animal").
The compiler does not call the default constructor of the superclass in this case.

NVSU-FR-ICD-05-00 (081220) Page 12 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

8. Protected members in inheritance

In Java, if a class includes protected fields and methods, then these fields and methods are
accessible from the subclass of the class.

Example 11: The use of super()


class Animal
{
protected String name;

protected void display()


{
[Link]("I am an animal.");
}
}

class Dog extends Animal


{
public void getInfo()
{
[Link]("My name is " + name);
}
}
public class Main
{
public static void main(String[] args)
{
// create an object of the subclass
Dog labrador = new Dog();

// access protected field and method


// using the object of subclass
[Link] = "Rocky";
[Link]();
[Link]();
}
}

Output Example 11
I am an animal.
My name is Rocky

In the above example, we have created a class named Animal. The class includes a protected field:
name and a method: display().

We have inherited the Dog class inherits Animal. Here, we are able to access the protected field and
method of the superclass using the labrador object of the subclass.

9. The “final” keyword

If you don't want other classes to inherit from a class, use the final keyword.

Syntax
final class Vehicle
{
...
}

NVSU-FR-ICD-05-00 (081220) Page 13 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

VI. EVALUATION (Note: Not to be included in the student’s copy of the IM)

VII. LEARNING ACTIVITIES


1. Create your own class that illustrates the use of inheritance as shown in this module. Make sure to
include class attributes and methods in your superclass and also include the concept of overriding,
super and other important concepts in inheritance in your program. I encourage everyone to create
your original program. Scores will be divided to the number of students who will submit the same
solution.
VIII. ASSIGNMENT
1. In your own words explain how the keyword “super” is used in applying inheritance in OOP.

IX. REFERENCES
Books:
Wu C.T. (2008). A Comprehensive Introduction to Object-Oriented Programming with Java. The
McGraw-Hill Companies, Inc.
Wu C.T. (2009). An Introduction to Object-Oriented Programming with Java. (5 th ed). The
McGraw-Hill Companies, Inc.

Leuck D., Niemeyer P., Loy M. (2020). Learning Java - An Introduction to Real-World
Programming with Java. (5th ed). O’Reilly Media, Inc.

Dale N., Joyce D., Weems C. (2002). Object-Oriented Data Structures Using Java. Jones and
Bartlett Publishers, Inc.
Skrien D. (2008). Object-Oriented Design Using Java. The McGraw-Hill Companies, Inc.
Poo D., Kiong D., Ashok S. (2007). Object-Oriented Programming and Java. (2nd ed). Springer.
Holmes B., Joyce D. (2000). Object-Oriented Programming Learning. (2nd ed). Jones And Bartlett
Publishers Inc.
Liskov B., Guttag J. (2000). Program Development in Java_ Abstraction, Specification, and
Object-Oriented Design. Addison Wesley Publishing.

Online Resources:

Java Programming Tutorial Object-oriented Programming (OOP) Basics. (2020) Retrieved from
[Link]
Introduction to Object-Oriented Programming. (n.d.) Retrieved from [Link]
/introduction/chapter-1---introduction-to-object-oriented-programming

Inheritance in Java. (n.d). Retrieved from [Link]

Java Inheritance.(n.d). Retrieved from [Link]

Java Method Overriding. (n.d.) Retrieved from [Link]


/method -overriding

Java super. (n.d) Retrieved from [Link]

The final Keyword. (n.d.) Retrived from [Link]

Super keyword in java with example. (n.d). Retrieved from [Link]


super-keyword-in-java-with-example/

NVSU-FR-ICD-05-00 (081220) Page 14 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 06: CPE3-2S-2024-2025

Prepared by:

RYAN VICTOR D. BAUTISTA


Faculty
Recommending Approval:

RYAN VICTOR D. BAUTISTA


Chair, Computer Engineering Department
Approved by:

LARRY P. REMOLAZO
OIC-Dean, College of Engineering

NVSU-FR-ICD-05-00 (081220) Page 15 of 15


“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”

You might also like