Inheritance: Super Class, Sub-Class, Method Overloading vs Overriding & Abstract Classes
1. Inheritance Basics: Super Class and Sub-Class
Inheritance is one of the four core pillars of OOPs in which a new class acquires the fields and methods of an existing class. It represents an IS-A relationship (e.g., a Dog IS-A Animal; a Manager IS-A Employee).
- Super Class (Parent Class / Base Class): The existing class whose properties and methods are inherited.
- Sub-Class (Child Class / Derived Class): The new class that inherits from the super class and can also add its own unique fields and methods.
extendsKeyword: Used to create a subclass in Java.
class SuperClassName {
// Parent fields and methods
}
class SubClassName extends SuperClassName {
// Inherits parent members + defines new child members
}
Why Use Inheritance?
- Code Reusability: Write common code once in the Super Class and reuse it across multiple Sub-Classes without copy-pasting.
- Method Overriding (Runtime Polymorphism): Allows a subclass to provide a specialized implementation of a method defined in its superclass.
2. Types of Inheritance in Java
Java supports 3 types of inheritance directly through classes:
Why doesn’t Java support Multiple Inheritance through Classes? Suppose Class
Ctries to inherit from both ClassAand ClassB(class C extends A, B), and bothAandBhave a method with the exact same signaturevoid show(). Whenc.show()is called, the compiler faces ambiguity over whichshow()to call (known as the Diamond Problem). To keep Java simple and bug-free, multiple inheritance of classes is prohibited and is instead achieved safely using Interfaces.
3. The super Keyword in Java
Whenever a subclass wants to refer to its immediate parent class (Super Class), it uses the super keyword.
3 Uses of the super Keyword:
super.variableName: Accesses a parent class instance variable when the child class has a variable with the same name.super.methodName(): Calls the parent class version of an overridden method.super(...)(Constructor Call): Invokes the constructor of the parent class from inside the child class constructor. (Rule:super(...)must be the very first statement inside the child constructor!)
class Employee {
String name;
Employee(String name) {
this.name = name;
}
void work() {
System.out.println(name + " is working in the company.");
}
}
class Programmer extends Employee {
String language;
Programmer(String name, String language) {
super(name); // 1. Calls Parent (Employee) constructor first!
this.language = language;
}
void display() {
super.work(); // 2. Calls Parent's work() method
System.out.println(name + " codes in " + language);
}
}
4. Method Overloading vs Method Overriding
Students often confuse Method Overloading and Method Overriding. Let’s make the distinction crystal clear!
A. Method Overloading (Compile-Time Polymorphism)
Defining multiple methods with the exact same name inside the same class, provided their parameter lists are different (different number of parameters, different data types of parameters, or different sequence of types).
- Note: Overloading cannot be achieved by changing only the return type!
class Calculator {
// 1. Overloaded method with 2 int parameters
int add(int a, int b) {
return a + b;
}
// 2. Overloaded method with 3 int parameters
int add(int a, int b, int c) {
return a + b + c;
}
// 3. Overloaded method with 2 double parameters
double add(double a, double b) {
return a + b;
}
}
B. Method Overriding (Runtime Polymorphism)
When a Sub-Class defines a method with the exact same name, exact same parameters, and same return type as a method already present in its Super Class, the child’s method overrides (replaces) the parent’s method at runtime.
Comparison Table: Method Overloading vs Method Overriding
| Basis | Method Overloading | Method Overriding |
|---|---|---|
| 1. Definition | Multiple methods with the same name but different parameters in the same class. | Subclass redefining a method with the exact same name and parameters as its Superclass. |
| 2. Where It Occurs | Within the same class. | Between two classes connected by Inheritance (Superclass & Subclass). |
| 3. Parameters | Parameter list MUST be different. | Parameter list MUST be identical. |
| 4. Return Type | Can be the same or different. | Must be the same (or covariant type). |
| 5. Polymorphism Type | Compile-Time Polymorphism (Early / Static Binding). | Runtime Polymorphism (Late / Dynamic Binding). |
6. private / static / final |
private, static, and final methods can be overloaded. |
private, static, and final methods cannot be overridden. |
5. Abstract Classes and Abstract Methods
Sometimes a Super Class is so general that it only defines what an action is, while leaving how to perform it to its child classes. For example, every Shape has an area(), but there is no single formula for a generic “Shape”—only specific subclasses like Circle (π r^2) and Rectangle (l × b) know how to calculate their area!
Rules of an abstract Class in Java:
- Declared using the
abstractkeyword (abstract class Shape). - Cannot be instantiated: You cannot create an object of an abstract class (
new Shape()gives a compile error). - Can contain both:
- Abstract Methods: Methods declared without a body (
abstract void calculateArea();). - Concrete (Normal) Methods: Regular methods with a body, plus constructors and instance variables.
- Abstract Methods: Methods declared without a body (
- Any Sub-Class that extends an abstract class must override and implement all its abstract methods (or declare itself
abstract).
Complete Example of an Abstract Class
// Abstract Super Class
abstract class Shape {
// Abstract method (no body, ends with a semicolon)
abstract void printArea();
// Normal concrete method
void info() {
System.out.println("Calculating area of the shape...");
}
}
// Sub-Class 1
class Circle extends Shape {
double radius = 5.0;
@Override
void printArea() {
System.out.println("Area of Circle = " + (3.14 * radius * radius));
}
}
// Sub-Class 2
class Rectangle extends Shape {
int length = 10, breadth = 4;
@Override
void printArea() {
System.out.println("Area of Rectangle = " + (length * breadth));
}
}
public class AbstractDemo {
public static void main(String[] args) {
// Shape s = new Shape(); // ERROR: Shape is abstract; cannot be instantiated!
Shape s1 = new Circle();
s1.info();
s1.printArea();
Shape s2 = new Rectangle();
s2.printArea();
}
}
💡 Exam Preparation Tips for DAVV BCA
- Always mention the 3 uses of the
superkeyword (super.var,super.method(),super()) when answering questions on Inheritance. - The
Shape→Circle/Rectanglecode example is the cleanest and easiest program to write in the exam for both Inheritance and Abstract Classes!