JAVA PROGRAMMING
Unit 2.1 — Inheritance
Complete Study Notes
Table of Contents
TOC \h \o "1-3"
1. Concept of Inheritance
🎯 Learning Objectives
✔ Understand what inheritance is and why it is used in Java.
✔ Differentiate between a superclass and a subclass.
✔ Use the extends keyword to establish inheritance.
✔ Recognize the advantages of inheritance such as code reusability.
Inheritance is one of the four fundamental pillars of Object-Oriented Programming (OOP), alongside
encapsulation, polymorphism, and abstraction. It is a mechanism by which one class (called the
child class or subclass) acquires the properties and behaviours (fields and methods) of another
class (called the parent class or superclass).
In real life, we often think of objects in terms of categories — for example, a Dog is a kind of Animal.
Inheritance in Java mirrors this real-world relationship. The subclass inherits all non-private
members of the superclass, which promotes code reusability and reduces redundancy in large
programs.
1.1 Key Terminology
• Superclass (Parent Class): The class whose properties and methods are inherited.
• Subclass (Child Class): The class that inherits from the superclass.
• extends keyword: Used in Java to declare inheritance.
• IS-A Relationship: Inheritance models an IS-A relationship (e.g., a Car IS-A Vehicle).
1.2 Advantages of Inheritance
1. Code Reusability: Write common code in the parent class and reuse it in child classes.
2. Method Overriding: Child classes can provide specific implementations for parent methods.
3. Extensibility: Existing code can be extended without modification.
4. Reduces Redundancy: Eliminates duplication of code across related classes.
Syntax
class SuperClass {
// fields and methods
}
class SubClass extends SuperClass {
// additional fields and methods
}
Example
// Superclass
class Animal {
String name;
void eat() {
[Link](name + " is eating.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
[Link](name + " is barking.");
}
}
// Main class
class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link] = "Bruno";
[Link](); // Inherited from Animal
[Link](); // Defined in Dog
}
}
📝 Note: Java supports single, multilevel, and hierarchical inheritance through classes, but does
NOT support multiple inheritance through classes (to avoid the Diamond Problem). Multiple
inheritance is achieved through interfaces.
2. Types of Inheritance
🎯 Learning Objectives
✔ Identify and explain the three types of inheritance supported by Java classes.
✔ Implement single, multilevel, and hierarchical inheritance with examples.
✔ Understand why multiple inheritance is not supported in Java.
Java supports three types of inheritance through classes: Single Inheritance, Multilevel Inheritance,
and Hierarchical Inheritance. Each type defines a different relationship structure between classes.
2.1 Single Inheritance
In single inheritance, one subclass inherits from exactly one superclass. This is the simplest and
most commonly used form of inheritance. The child class gains access to all public and protected
members of the parent class.
Syntax
class Parent {
// parent members
}
class Child extends Parent {
// child members
}
Example
class Vehicle {
int speed;
void showSpeed() {
[Link]("Speed: " + speed + " km/h");
}
}
class Car extends Vehicle {
String brand;
void showBrand() {
[Link]("Brand: " + brand);
}
}
class Main {
public static void main(String[] args) {
Car c = new Car();
[Link] = 120;
[Link] = "Toyota";
[Link]();
[Link]();
}
}
2.2 Multilevel Inheritance
In multilevel inheritance, a class is derived from a class that is itself derived from another class,
forming a chain of inheritance. This creates a parent-child-grandchild relationship. Each level adds
more specific characteristics to the hierarchy.
Syntax
class A {
// members of A
}
class B extends A {
// members of B (inherits A)
}
class C extends B {
// members of C (inherits A and B)
}
Example
class Animal {
void breathe() {
[Link]("Breathing...");
}
}
class Mammal extends Animal {
void feedMilk() {
[Link]("Feeding milk to young...");
}
}
class Human extends Mammal {
void speak() {
[Link]("Speaking...");
}
}
class Main {
public static void main(String[] args) {
Human h = new Human();
[Link](); // From Animal
[Link](); // From Mammal
[Link](); // From Human
}
}
📝 Note: In multilevel inheritance, a subclass at any level inherits all public and protected members
from every class above it in the hierarchy.
2.3 Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single superclass. All child classes
share the common properties and methods defined in the parent class, but each child can also
have its own unique properties and methods.
Syntax
class Parent {
// shared members
}
class Child1 extends Parent { /* ... */ }
class Child2 extends Parent { /* ... */ }
class Child3 extends Parent { /* ... */ }
Example
class Shape {
void draw() {
[Link]("Drawing a shape...");
}
}
class Circle extends Shape {
void area() {
[Link]("Area = pi * r * r");
}
}
class Rectangle extends Shape {
void area() {
[Link]("Area = length * breadth");
}
}
class Triangle extends Shape {
void area() {
[Link]("Area = 0.5 * base * height");
}
}
3. Method Overriding
🎯 Learning Objectives
✔ Define method overriding and explain when it is used.
✔ Implement method overriding correctly in Java.
✔ Understand the rules that govern method overriding.
✔ Distinguish between method overriding and method overloading.
Method overriding occurs when a subclass provides its own specific implementation of a method
that is already defined in its superclass. The overriding method in the subclass must have the same
name, same return type, and the same parameter list as the method in the superclass.
Method overriding is a key feature that enables runtime polymorphism (also called dynamic method
dispatch) in Java. When an overridden method is called through a superclass reference pointing to
a subclass object, the JVM determines at runtime which version of the method to execute.
3.1 Rules for Method Overriding
• The method name must be the same as in the superclass.
• The parameter list must be identical — same number, types, and order.
• The return type must be the same or a subtype (covariant return type).
• The access modifier cannot be more restrictive than the overridden method.
• Static methods cannot be overridden (they are hidden, not overridden).
• Final methods cannot be overridden.
• Private methods cannot be overridden (they are not inherited).
Example
class Animal {
void sound() {
[Link]("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog says: Woof!");
}
}
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat says: Meow!");
}
}
class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link](); // Output: Dog says: Woof!
a = new Cat();
[Link](); // Output: Cat says: Meow!
}
}
📝 Note: The @Override annotation is not mandatory but is recommended. It tells the compiler to
verify that the method actually overrides a superclass method, helping catch errors at compile time.
4. Final Variables and Final Methods
🎯 Learning Objectives
✔ Explain the use of the final keyword in Java.
✔ Declare and use final variables as constants.
✔ Understand why final methods cannot be overridden.
✔ Use final classes to prevent inheritance.
The final keyword in Java is a non-access modifier that can be applied to variables, methods, and
classes. Its meaning differs slightly depending on where it is applied, but the underlying concept is
always the same: once set, it cannot be changed or extended.
4.1 Final Variables
A final variable is a constant — its value can be assigned only once. After initialization, attempting
to reassign it results in a compile-time error. By convention, final variable names are written in
UPPER_CASE with underscores separating words (e.g., MAX_SIZE).
Syntax
final dataType VARIABLE_NAME = value;
Example
class Circle {
final double PI = 3.14159;
double radius = 7.0;
double area() {
return PI * radius * radius;
}
}
class Main {
public static void main(String[] args) {
Circle c = new Circle();
[Link]("Area = " + [Link]());
// [Link] = 3.0; // ERROR: cannot assign to final variable
}
}
4.2 Final Methods
A method declared as final cannot be overridden by any subclass. Final methods are used when
you want to guarantee that the implementation of a method remains unchanged in all subclasses,
ensuring consistent behaviour across the inheritance hierarchy.
Syntax
class Parent {
final void display() {
[Link]("This is a final method.");
}
}
class Child extends Parent {
// void display() { } // ERROR: cannot override final method
}
4.3 Final Classes
A class declared as final cannot be subclassed (extended). This is used to prevent inheritance
entirely — for example, the [Link] class in Java is final, ensuring that no one can alter the
core behaviour of String objects.
Syntax
final class ImmutableData {
// members
}
// class SubData extends ImmutableData { } // ERROR: cannot inherit
from final class
5. Use of super Keyword
🎯 Learning Objectives
✔ Explain the purpose of the super keyword in Java.
✔ Use super to access superclass constructors.
✔ Use super to call overridden methods of the parent class.
✔ Access hidden superclass fields using super.
The super keyword in Java is a reference variable that is used to refer to the immediate parent
class of the current object. It serves three primary purposes: calling the parent class constructor,
accessing parent class methods, and accessing parent class fields that are hidden by the subclass.
5.1 super() — Calling the Superclass Constructor
When a subclass object is created, the superclass constructor must be invoked. Java automatically
inserts a call to super() (no-arg constructor) if not explicitly specified. To call a parameterized parent
constructor, super() must be the first statement in the subclass constructor.
Example
class Person {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
}
class Student extends Person {
int rollNo;
Student(String name, int age, int rollNo) {
super(name, age); // Calls Person's constructor
[Link] = rollNo;
}
void display() {
[Link]("Name: " + name + ", Age: " + age + ", Roll:
" + rollNo);
}
}
class Main {
public static void main(String[] args) {
Student s = new Student("Alice", 20, 101);
[Link]();
}
}
5.2 [Link]() — Calling Overridden Methods
When a method is overridden in a subclass, the overridden version in the parent class can still be
accessed using [Link](). This is useful when you want to extend (rather than
completely replace) the parent's functionality.
Example
class Employee {
void displayInfo() {
[Link]("Employee Information:");
}
}
class Manager extends Employee {
@Override
void displayInfo() {
[Link](); // Calls parent version
[Link]("Role: Manager");
}
}
class Main {
public static void main(String[] args) {
Manager m = new Manager();
[Link]();
}
}
5.3 [Link] — Accessing Parent Class Fields
If a subclass declares a field with the same name as a field in the superclass, the parent's field is
hidden. The super keyword allows access to the hidden field of the parent class.
Example
class Parent {
String type = "Parent";
}
class Child extends Parent {
String type = "Child";
void showTypes() {
[Link]("Child type: " + type);
[Link]("Parent type: " + [Link]);
}
}
6. Abstract Methods and Classes
🎯 Learning Objectives
✔ Define an abstract class and understand when to use it.
✔ Declare and implement abstract methods.
✔ Understand that abstract classes cannot be instantiated.
✔ Distinguish between abstract classes and interfaces.
✔ Apply abstraction to design flexible and extensible class hierarchies.
Abstraction is the concept of hiding implementation details and showing only the essential features
of an object. In Java, abstraction is achieved using abstract classes and interfaces. An abstract
class is a class that cannot be instantiated on its own and may contain a mix of abstract methods
(without a body) and concrete methods (with a body).
6.1 Abstract Class
An abstract class is declared using the abstract keyword. It acts as a blueprint for its subclasses.
Any class that contains at least one abstract method must itself be declared abstract. A subclass
that extends an abstract class must provide implementations for all abstract methods, unless the
subclass is also declared abstract.
Key Characteristics
• Declared with the abstract keyword.
• Cannot be instantiated directly (new AbstractClass() is not allowed).
• May contain abstract methods (no body) and concrete methods (with body).
• Can have constructors, static methods, and final methods.
• A subclass must implement all abstract methods, or itself be abstract.
Syntax
abstract class ClassName {
abstract returnType methodName(parameters); // abstract method
returnType concreteMethod() { // concrete method
// body
}
}
6.2 Abstract Method
An abstract method is a method that is declared without an implementation. It consists only of the
method signature (declaration), and the body is left to be provided by the concrete subclasses.
Abstract methods can only exist inside abstract classes.
Example — Abstract Class and Method
abstract class Shape {
String color;
// Abstract method - no body
abstract double area();
// Concrete method
void displayColor() {
[Link]("Color: " + color);
}
}
class Circle extends Shape {
double radius;
Circle(double r) { [Link] = r; }
@Override
double area() {
return [Link] * radius * radius;
}
}
class Rectangle extends Shape {
double length, breadth;
Rectangle(double l, double b) { length = l; breadth = b; }
@Override
double area() {
return length * breadth;
}
}
class Main {
public static void main(String[] args) {
Shape s;
s = new Circle(5.0);
[Link]("Circle Area: " + [Link]());
s = new Rectangle(4.0, 6.0);
[Link]("Rectangle Area: " + [Link]());
}
}
📝 Note: You can declare a reference variable of an abstract class type, but you cannot create an
object using new on an abstract class. This is a fundamental rule in Java abstraction.
6.3 Abstract Class vs Interface
Feature Abstract Class Interface
Keyword abstract class interface
Methods Abstract + concrete Abstract (default/static in Java 8+)
Variables Any type public static final only
Constructor Yes No
Inheritance extends (single) implements (multiple)
Access Modif. Any modifier public by default
7. Summary
This unit covered the concept of inheritance in Java and its three main types — single, multilevel,
and hierarchical. Key mechanisms such as method overriding, the final keyword, and the super
keyword were explored in depth. Finally, abstraction through abstract classes and methods was
discussed, demonstrating how Java enforces a contract between a parent class and its subclasses.
Topic Key Point
Inheritance Subclass acquires properties of superclass using extends.
Single Inheritance One class inherits from exactly one parent class.
Multilevel Chain of inheritance — A -> B -> C.
Hierarchical Multiple child classes inherit from one parent class.
Method Overriding Subclass redefines parent method with same signature.
final Variable Value assigned once; cannot be changed.
final Method Cannot be overridden by any subclass.
final Class Cannot be subclassed / extended.
super Keyword Refers to immediate parent class constructor, method, or field.
Abstract Class Cannot be instantiated; may contain abstract methods.
Abstract Method Declared without body; must be implemented by subclass.
— End of Unit 2.1 —