Inheritance
Prepared by
Bolisay, Karlo Cris G.
What is Inheritance?
• Inheritance in Java is one of the core principles of Object-
Oriented Programming (OOP).
• It allows a class (called the subclass or child class) to
acquire the properties (fields/attributes) and behaviors
(methods) of another class (called the superclass or
parent class).
What is Inheritance?
• This helps in code reusability, method overriding, and
establishing a natural hierarchy between classes.
What is Inheritance?
• The keyword extends is used to create a child class
(subclass) from a parent class (superclass). It literally
means "this class inherits from another class."
What is Inheritance?
• To further understand the concept, think of a school as a
system of roles. We have 2 classes, Student and Teacher.
Every Person has common characteristics.
Sample
Code
for
[Link]
Sample
Code
for
[Link]
What is Inheritance?
• A student is still a person, but has extra features like
study().
• A teacher is also a person, but has extra features like
teach().
Sample
Code
for
[Link]
Output
for
[Link]
Types of
Inheritance
Types of Inheritance
• We have three (3) types of inheritance:
• Single Inheritance – One class inherits from another.
Sample
Code
for
[Link]
Types of Inheritance
• Multilevel Inheritance – A class inherits from another, and
another class inherits from it.
Sample
Code
for
[Link]
Types of Inheritance
• In this example, it demonstrates that Principal IS-A
Teacher, and also IS-A Person.
Types of Inheritance
• Hierarchical Inheritance – Multiple classes inherit from
the same superclass.
Sample
Code
for
[Link]
Types of Inheritance
• To summarize:
• Single Inheritance: Person → Student
• Multilevel Inheritance: Person → Teacher → Principal
• Hierarchical Inheritance: Person → Student and Person →
Teacher
Protected
Keyword
Protected Keyword
• As discussed on our previous lessons, access modifiers
control the visibility of variables and methods. One of
these is protected, which plays a special role in
inheritance.
Protected Keyword
• A protected member can be accessed:
✅ Inside the same class
✅ Inside the same package
✅ By subclasses (even if they are in different packages)
• This makes protected especially important when working
with inheritance, because it allows child classes to use
parent class members directly.
Sample
Code
for
[Link]
(inside general package)
Sample
Code
for
[Link]
(inside school package)
Sample
Code
for
[Link]
(inside college package)
Output
for
[Link]
Protected Keyword
• Now, the question is: why use protected and not just
private and use getter/setter methods?
• We use protected and private (with getter/setter
methods) based on the requirements.
Protected Keyword
• Use protected when subclasses need direct access to
parent fields/methods, especially in frameworks,
libraries, or base classes where flexibility and less
boilerplate code than writing getters/setters for
everything.
Protected Keyword
• Use getters/setters when you need strong encapsulation,
data safety, or the ability to control and change how fields
are accessed/modified in the future.
Protected Keyword
• To summarize:
• Use protected when you want to design for inheritance
and allow subclasses more freedom.
• Use getters/setters when you want stronger
encapsulation and don’t fully trust or intend subclasses
to manipulate data directly.
Why Use
Inheritance?
Why Use Inheritance?
• Code Reusability – Instead of writing the same code
again, you can put common properties and methods in a
superclass and reuse them in subclasses.
• Example: All animals eat and sleep, so you define these
in Animal and reuse in Dog, Cat, etc.
Why Use Inheritance?
• Improves Maintainability – If something changes (like
updating how eat() works), you only modify the
superclass instead of editing every subclass.
Why Use Inheritance?
• Method Overriding (Polymorphism) – Inheritance enables
runtime polymorphism, where a subclass can provide its
own version of a method from the superclass.
Sample
Code
for
[Link]
Why Use Inheritance?
• Establishes Relationships ("is-a") – Inheritance models
real-world relationships.
• Example: Dog is an Animal, Student and Teacher is a
Person. This makes the design more natural and intuitive.
Why Use Inheritance?
• Encourages DRY Principle ("Don't Repeat Yourself") –
Shared code is written once in the parent class.
• Subclasses inherit and specialize only what’s different.
References
1. Horstmann, C. S., & Cornell, G. (2023). Core Java Volume I: Fundamentals (12th ed.).
Prentice Hall.
2. Schildt, H. (2023). Java: The complete reference (12th ed.). McGraw-Hill Education.
3. Sierra, K., & Bates, B. (2022). Head first Java (3rd ed.). O’Reilly Media.
4. KTByte. (n.d.). Extends super. In Interactive algorithms textbook in Java. KTByte.
[Link]
5. Oracle. (n.d.). Inheritance. In The Java™ tutorials: Learning the Java language — Interfaces &
inheritance. Oracle. [Link]
6. TechVidvan. (n.d.). Types & importance of inheritance with real-life examples!. TechVidvan.
[Link]
7. W3Schools. (n.d.). Java inheritance (subclass and superclass). W3Schools.
[Link]
References
8. BeginnersBook. (2024, November 30). Inheritance in Java with examples. BeginnersBook.
[Link]
9. GeeksforGeeks. (2025, August). Inheritance in Java. GeeksforGeeks.
[Link]
10. Java-Programming MOOC. (n.d.). Class inheritance. Java-Programming MOOC. [Link]
[Link]/part-9/1-inheritance
11. Programiz. (n.d.). Java inheritance (with examples). Programiz.
[Link]
12. Shiksha. (2024, November). Types of inheritance in Java. Shiksha.
[Link]
156091
Thank you!