📘 Java Inheritance Notes
🔹 1. What is Inheritance?
Inheritance is a feature in object-oriented programming that allows a class (subclass/child)
to inherit attributes and methods from another class (superclass/parent).
Think of it like passing traits from a parent to a child.
🔹 2. Why Use Inheritance?
• Code Reuse: Avoid duplicating code by reusing existing functionality.
• Extensibility: Extend existing classes with additional features.
• Polymorphism: Treat different objects in a common way (e.g., through a superclass
reference).
🔹 3. Basic Syntax
class Superclass { //
fields and methods
} class Subclass extends Superclass
{ // additional fields and
methods } Example:
class Animal {
void makeSound() {
[Link]("Some sound");
}
} class Dog extends
Animal { void bark() {
[Link]("Woof!");
}
}
🔹 4. The super Keyword
• super() – calls the superclass constructor.
• [Link]() – calls a method from the superclass.
Example:
class Person {
String name;
Person(String name) {
[Link] = name;
}
} class Student extends
Person {
Student(String name) {
super(name); // Call Person constructor
}
}
🔹 5. Overriding Methods
A subclass can change the behavior of a method it inherits.
class Person {
void display() {
[Link]("Person");
}
}
class Student extends Person {
@Override
void display() {
[Link]("Student");
} }
Use @Override to show you're intentionally replacing the method.
🔹 6. Access Modifiers in Inheritance
Modifier Accessible in Subclass? Accessible Outside Package?
private ❌ ❌
protected ✅ (in subclass) ❌ (unless subclassed)
public ✅ ✅
(no modifier) ❌ (if in another package) ❌
🔹 7. Constructors and Inheritance
• Constructors are not inherited.
• The subclass constructor can call the superclass constructor using super() as the
first line.
🔹 8. Polymorphism (via Inheritance)
Allows objects of different subclasses to be treated as objects of a common superclass.
Example:
class Animal {
void speak() {
[Link]("Animal sound");
}
} class Cat extends
Animal { void speak()
{
[Link]("Meow");
}
}
Animal a = new Cat();
[Link](); // Output: Meow
🔹 9. Abstract Classes
• Cannot be instantiated.
• May contain abstract methods (no body).
• Subclasses must implement all abstract methods.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing circle");
}
}
🔹 10. Interfaces (Compared to Inheritance)
• Java supports single inheritance (only one superclass).
• Use interfaces to simulate multiple inheritance.
interface Drawable {
void draw();
}
class Picture implements Drawable {
public void draw() {
[Link]("Drawing picture");
}
}
Common Exam Concepts (IEB NSC)
• Constructor chaining: using super(...)
• Overriding methods: especially toString() and custom methods
• Polymorphism: method calls through superclass references
• Access modifiers: identifying what fields/methods are accessible
• Abstract classes and interfaces: identifying which to use when
📝 Tips for Practicals
1. Always use @Override for overridden methods.
2. Check if you’re required to call the parent constructor.
3. Use [Link]() to reuse parent logic if needed.
4. Understand when to use abstract classes vs interfaces.
5. Test all classes using a main or Test class.
✅ Practice Prompt
Create an abstract class Employee with method calculatePay().
Subclasses HourlyWorker and SalariedWorker implement it. Test
using an Employee[] array and polymorphism.