Java Inheritance – Complete Lecture
Notes
Lecture Outline
1. What is Inheritance?
2. Why Use Inheritance?
3. Syntax & Terminology
4. Types of Inheritance
5. super and this Keywords
6. Constructor Behavior
7. Method Overriding
8. Access Control & Inheritance
9. The final Keyword
10. Real-life Analogy
11. Common Interview Questions
12. Exercises
1. What is Inheritance?
Inheritance is a mechanism by which one class (subclass/child) derives properties and
behaviors (fields and methods) from another class (superclass/parent). It supports code
reuse, polymorphism, and hierarchical classification.
class Parent {
/* fields and methods */
}
class Child extends Parent {
/* additional fields and methods */
}
2. Why Use Inheritance?
Code Reusability – avoid duplication.
Method Overriding – customize inherited behavior.
Polymorphism – treat objects uniformly.
Logical Hierarchy – model “is-a” relationships.
3. Syntax & Terminology
Basic structure in Java:
class Parent {
void display() {
[Link]("Hello from Parent");
}
}
class ChildA extends Parent {
void show() {
[Link]("Hello from Child A");
}
}
Key Terms:
Superclass – the class being inherited from.
Subclass – the class that inherits.
`extends` – keyword establishing inheritance.
4. Types of Inheritance
Java supports Single, Multilevel, and Hierarchical inheritance with classes. Multiple and
Hybrid inheritance are achieved via interfaces.
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
5. super and this Keywords
`super` refers to the parent class, used to access parent methods or constructors. `this`
refers to the current object, used to access current class members or chain constructors.
class Animal {
Animal() {
[Link]("Animal constructor");
}
void sound() {
[Link]("Generic sound");
}
}
class Dog extends Animal {
Dog() {
super(); // calls Animal constructor
}
@Override
void sound() {
[Link]();
[Link]("Dog barks");
}
}
6. Constructor Behavior
Constructors are not inherited. However, the superclass constructor is invoked (implicitly
or explicitly with `super()`) when a subclass object is created.
7. Method Overriding
When a subclass provides its own implementation of a method already defined in its
superclass using the same name, return type, and parameters.
class Animal {
void sound() {
[Link]("Generic animal sound");
}
}
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat meows");
}
}
8. Access Control & Inheritance
Accessibility of members:
Modifier Same Class Subclass Subclass Outside class
(same pkg) (diff pkg)
public ✔ ✔ ✔ ✔
protected ✔ ✔ ✔ ✖
default ✔ ✔ ✖ ✖
private ✔ ✖ ✖ ✖
9. The final Keyword
`final class` – cannot be extended.
`final method` – cannot be overridden.
`final variable` – value cannot change (constant).
final class Constants {}
class Vehicle {
final void start() {}
}
final int MAX_SPEED = 120;
10. Real-life Analogy
A `Car` is-a `Vehicle`. Hence, it inherits the behaviors (e.g., `start()`) defined in `Vehicle` and
may add its own behaviors like `drive()`.
class Vehicle {
void start() {
[Link]("Starting...");
}
}
class Car extends Vehicle {
void drive() {
[Link]("Driving...");
}
}
11. Common Interview Questions
1. Does Java support multiple inheritance with classes? Why not?
2. Can constructors be inherited?
3. Difference between `super` and `this`?
4. Can private members be inherited?
5. What is method overriding?
12. Exercises
Test your understanding with these exercises:
Write a class hierarchy for `Employee` → `Manager` → `SeniorManager` demonstrating
multilevel inheritance. Include one overridden method in each subclass.
Explain why Java prohibits multiple inheritance with classes but allows it with
interfaces.
Create an interface `Flyable` and a class `Bird` that extends `Animal` and implements
`Flyable`. Show method overriding and interface implementation.
Given the following code, predict the output and explain:
class A { void show() { [Link]("A"); } }
class B extends A { void show() { [Link]("B"); } }
class C extends B { }
public class Test { public static void main(String[] args) { A obj = new C(); [Link](); } }
State true or false:
a) A `final class` can be extended.
b) Private methods are inherited but not accessible.
c) `super` can be used to access variable shadowed by subclass.
Summary
Inheritance underpins object-oriented design in Java. It enables code reuse, hierarchical
organization, and polymorphic behavior. Mastering keywords like `extends`, `super`, and
`final`, understanding constructor chaining, and knowing access modifiers are essential for
effective Java development.