Module 2: Object-Oriented Design: Classes,
Inheritance, and Polymorphism
Premanand S
Assistant Professor
School of Electronics Engineering
Vellore Institute of Technology
Chennai Campus
premanand.s@[Link]
February 24, 2026
Premanand S (VIT-CC) Module 1 February 24, 2026 1 / 67
Understanding Super() – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 2 / 67
What is super()?
Definition
super() is used to invoke the constructor of the immediate parent class.
Used inside constructors only
Ensures parent initialization
Supports constructor chaining
Premanand S (VIT-CC) Module 1 February 24, 2026 3 / 67
Why super() is Required
Parent setup must happen first
Child depends on parent data
Java enforces safe construction order
Premanand S (VIT-CC) Module 1 February 24, 2026 4 / 67
Rules of super()
Must be first statement
Cannot be used in methods
Calls only immediate parent constructor
Mandatory if parent has no default constructor
Premanand S (VIT-CC) Module 1 February 24, 2026 5 / 67
Syntax for super()
super(); // calls parent default constructor
super(arg1); // calls parent parameterized constructor
Premanand S (VIT-CC) Module 1 February 24, 2026 6 / 67
Example of super()
class A {
A() {
[Link]("Parent Constructor");
} }
class B extends A {
B() {
super();
[Link]("Child Constructor");
}}
public class Main {
public static void main(String[] args) {
B obj = new B();
}}
Premanand S (VIT-CC) Module 1 February 24, 2026 7 / 67
Example of super()
Output?
class A {
A() {
[Link]("A");
}
}
class B extends A {
B() {
[Link]("B");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 8 / 67
Example of super()
Output?
class A {
A(int x) {
[Link](x);
}
}
class B extends A {
B() {
[Link]("B");
}}
Premanand S (VIT-CC) Module 1 February 24, 2026 9 / 67
Example of super()
Output?
class A {
A(int x) {
[Link]("A: " + x);
}
}
class B extends A {
B() {
super(10);
[Link]("B");
}
}
B obj = new B();
Premanand S (VIT-CC) Module 1 February 24, 2026 10 / 67
Example of super()
Output?
class B extends A {
B() {
[Link]("B");
super();
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 11 / 67
Understanding final – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 12 / 67
The final Keyword
final means cannot be changed
Used with variables, methods, and classes
Premanand S (VIT-CC) Module 1 February 24, 2026 13 / 67
Uses of final
final variable constant value
final method cannot override
final class cannot inherit
Premanand S (VIT-CC) Module 1 February 24, 2026 14 / 67
Why final is Important
Prevents misuse
Improves security
Ensures consistency
Premanand S (VIT-CC) Module 1 February 24, 2026 15 / 67
Example of final variable
final int MAX = 100;
MAX = 200;
Premanand S (VIT-CC) Module 1 February 24, 2026 16 / 67
Example of final method
class A {
final void show() {
[Link]("A");
}
}
class B extends A {
void show() { }
}
Premanand S (VIT-CC) Module 1 February 24, 2026 17 / 67
Example of final class
final class A { }
class B extends A { }
Premanand S (VIT-CC) Module 1 February 24, 2026 18 / 67
Confusion about final class
final does NOT mean immutable object It means reference cannot
change
final StringBuilder sb = new StringBuilder("Hi");
[Link](" Hello");
Premanand S (VIT-CC) Module 1 February 24, 2026 19 / 67
Understanding Polymorphism – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 20 / 67
What is Polymorphism?
Definition
Polymorphism means one object can take many forms.
Same interface
Different implementations
Premanand S (VIT-CC) Module 1 February 24, 2026 21 / 67
Types of Polymorphism
Compile-time Polymorphism (Overloading)
Runtime Polymorphism (Overriding)
Premanand S (VIT-CC) Module 1 February 24, 2026 22 / 67
Understanding Polymorphism-Types – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 23 / 67
Compile-Time Polymorphism
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
Decided at compile time
Premanand S (VIT-CC) Module 1 February 24, 2026 24 / 67
Runtime Polymorphism
class Animal {
void sound() {
[Link]("Animal sound");
} }
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
} }
Animal obj = new Dog();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 25 / 67
Method vs Variable Resolution
Methods runtime (object type)
Variables compile time (reference type)
class A {
void show() {
class A { [Link]("A");
int x = 10; }
} }
class B extends A { class B extends A {
int x = 20; void show() {
} [Link]("B");
A obj = new B(); }
[Link](obj.x); }
A obj = new B();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 26 / 67
Rules of Runtime Polymorphism
Inheritance required
Method overriding required
Parent reference to child object
Premanand S (VIT-CC) Module 1 February 24, 2026 27 / 67
Advantages of Polymorphism
Code flexibility
Extensibility
Loose coupling
Premanand S (VIT-CC) Module 1 February 24, 2026 28 / 67
Example for PM
class A {
void show() { [Link]("A"); }
}
class B extends A {
void show() { [Link]("B"); }
}
A obj = new B();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 29 / 67
Example for PM
class A {
static void show() { [Link]("A"); }
}
class B extends A {
static void show() { [Link]("B"); }
}
A obj = new B();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 30 / 67
Example for PM
class A {
final void show() { }
}
Premanand S (VIT-CC) Module 1 February 24, 2026 31 / 67
Understanding Abstract Class – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 32 / 67
What is an Abstract Class?
Definition
An abstract class is a class declared with the abstract keyword and
cannot be instantiated.
Abstract class tells WHAT must be done, not HOW it is done.
May contain abstract methods
May contain concrete methods
Premanand S (VIT-CC) Module 1 February 24, 2026 33 / 67
Why Abstract Class?
Enforce common structure
Achieve partial abstraction
Promote design consistency
Premanand S (VIT-CC) Module 1 February 24, 2026 34 / 67
Syntax
abstract class Vehicle {
abstract void start(); // abstract method
void stop() { // concrete method
[Link]("Stopping");
}}
class Car extends Vehicle {
void start() {
[Link]("Car starts with key");
}}
Vehicle v = new Car();
[Link]();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 35 / 67
Rules of Abstract Class
Cannot be instantiated
Abstract method has no body
Child must implement abstract methods
Can contain constructors
Premanand S (VIT-CC) Module 1 February 24, 2026 36 / 67
Example
abstract class Animal {
abstract void sound();
void sleep() {
[Link]("Sleeping");
}}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}}
Animal obj = new Dog();
[Link]();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 37 / 67
Tricky Q1: Abstract Class without Abstract Method
Question
Can an abstract class exist without any abstract methods?
abstract class A {
void show() {
[Link]("Hello");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 38 / 67
Tricky Q1: Abstract Class without Abstract Method
Question
Can an abstract class exist without any abstract methods?
abstract class A {
void show() {
[Link]("Hello");
}
}
Answer
Yes. An abstract class may not contain abstract methods.
Premanand S (VIT-CC) Module 1 February 24, 2026 38 / 67
Tricky Q2: Object Creation
Question
Can we create an object of an abstract class?
abstract class A { }
A obj = new A();
Premanand S (VIT-CC) Module 1 February 24, 2026 39 / 67
Tricky Q2: Object Creation
Question
Can we create an object of an abstract class?
abstract class A { }
A obj = new A();
Answer
No. Abstract classes cannot be instantiated.
Premanand S (VIT-CC) Module 1 February 24, 2026 39 / 67
Tricky Q3: Constructor in Abstract Class
Question
Can an abstract class have a constructor?
abstract class A {
A() {
[Link]("Constructor");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 40 / 67
Tricky Q3: Constructor in Abstract Class
Question
Can an abstract class have a constructor?
abstract class A {
A() {
[Link]("Constructor");
}
}
Answer
Yes. Constructor executes when a child object is created.
Premanand S (VIT-CC) Module 1 February 24, 2026 40 / 67
Tricky Q4: Static Methods
Question
Can an abstract class contain static methods?
abstract class A {
static void show() {
[Link]("Static Method");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 41 / 67
Tricky Q4: Static Methods
Question
Can an abstract class contain static methods?
abstract class A {
static void show() {
[Link]("Static Method");
}
}
Answer
Yes. Static methods belong to the class, not objects.
Premanand S (VIT-CC) Module 1 February 24, 2026 41 / 67
Tricky Q5: Private Abstract Method
Question
Can an abstract method be private?
abstract class A {
private abstract void show();
}
Premanand S (VIT-CC) Module 1 February 24, 2026 42 / 67
Tricky Q5: Private Abstract Method
Question
Can an abstract method be private?
abstract class A {
private abstract void show();
}
Answer
No. Abstract methods must be overridden; private methods cannot be
overridden.
Premanand S (VIT-CC) Module 1 February 24, 2026 42 / 67
Tricky Q6: Static Abstract Method
Question
Can an abstract method be static?
abstract class A {
abstract static void show();
}
Premanand S (VIT-CC) Module 1 February 24, 2026 43 / 67
Tricky Q6: Static Abstract Method
Question
Can an abstract method be static?
abstract class A {
abstract static void show();
}
Answer
No. Static methods cannot be overridden.
Premanand S (VIT-CC) Module 1 February 24, 2026 43 / 67
Tricky Q7: final and abstract Together
Question
Can a class be both final and abstract?
final abstract class A { }
Premanand S (VIT-CC) Module 1 February 24, 2026 44 / 67
Tricky Q7: final and abstract Together
Question
Can a class be both final and abstract?
final abstract class A { }
Answer
No. Abstract requires inheritance; final prevents inheritance.
Premanand S (VIT-CC) Module 1 February 24, 2026 44 / 67
Tricky Q8: Child Not Implementing Abstract Method
Question
What happens if a child class does not implement an abstract method?
abstract class A {
abstract void show();
}
class B extends A { }
Premanand S (VIT-CC) Module 1 February 24, 2026 45 / 67
Tricky Q8: Child Not Implementing Abstract Method
Question
What happens if a child class does not implement an abstract method?
abstract class A {
abstract void show();
}
class B extends A { }
Answer
Compilation error unless class B is declared abstract.
Premanand S (VIT-CC) Module 1 February 24, 2026 45 / 67
Tricky Q9: Abstract Class Inheritance
Question
Can an abstract class extend another abstract class?
abstract class A {
abstract void show();
}
abstract class B extends A { }
Premanand S (VIT-CC) Module 1 February 24, 2026 46 / 67
Tricky Q9: Abstract Class Inheritance
Question
Can an abstract class extend another abstract class?
abstract class A {
abstract void show();
}
abstract class B extends A { }
Answer
Yes. Responsibility can be passed to next level.
Premanand S (VIT-CC) Module 1 February 24, 2026 46 / 67
Tricky Q10: Polymorphism with Abstract Class
Question
Can we use abstract class reference for polymorphism?
abstract class A {
abstract void show();
}
class B extends A {
void show() {
[Link]("B");
}
}
A obj = new B();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 47 / 67
Tricky Q10: Polymorphism with Abstract Class
Question
Can we use abstract class reference for polymorphism?
abstract class A {
abstract void show();
}
class B extends A {
void show() {
[Link]("B");
}
}
A obj = new B();
[Link]();
Answer
Yes. Runtime polymorphism is supported.
Premanand S (VIT-CC) Module 1 February 24, 2026 47 / 67
Tricky Q11: main() in Abstract Class
Question
Can an abstract class contain main() method?
abstract class A {
public static void main(String[] args) {
[Link]("Hello");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 48 / 67
Tricky Q11: main() in Abstract Class
Question
Can an abstract class contain main() method?
abstract class A {
public static void main(String[] args) {
[Link]("Hello");
}
}
Answer
Yes. main() is static; object creation is not required.
Premanand S (VIT-CC) Module 1 February 24, 2026 48 / 67
Abstract Class Features
Variables allowed
Static methods allowed
Final methods allowed
Constructors allowed
Premanand S (VIT-CC) Module 1 February 24, 2026 49 / 67
Abstract vs Normal Class
Abstract Class Normal Class
Cannot instantiate Can instantiate
May have abstract methods No abstract methods
Premanand S (VIT-CC) Module 1 February 24, 2026 50 / 67
Understanding Interface – A Real-Life View
Premanand S (VIT-CC) Module 1 February 24, 2026 51 / 67
What is an Interface?
Definition
An interface is a blueprint that defines a set of methods without
implementation.
Complete abstraction
Defines behavior contract
Premanand S (VIT-CC) Module 1 February 24, 2026 52 / 67
Interface Syntax
interface Vehicle {
void start();
}
Premanand S (VIT-CC) Module 1 February 24, 2026 53 / 67
Implementing an Interface
class Car implements Vehicle {
public void start() {
[Link]("Car starts");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 54 / 67
Rules of Interface
Methods are public and abstract
Variables are public static final
No constructors allowed
Cannot be instantiated
Premanand S (VIT-CC) Module 1 February 24, 2026 55 / 67
Multiple Inheritance using Interface
interface A {
void showA();
}
interface B {
void showB();
}
class C implements A, B {
public void showA() { }
public void showB() { }
}
Premanand S (VIT-CC) Module 1 February 24, 2026 56 / 67
Interface vs Abstract Class
Interface Abstract Class
100% abstraction Partial abstraction
Multiple inheritance Single inheritance
No constructor Constructor allowed
Premanand S (VIT-CC) Module 1 February 24, 2026 57 / 67
Q1: Can We Create Object of Interface?
Question
Can we create an object of an interface?
interface A { }
A obj = new A();
Premanand S (VIT-CC) Module 1 February 24, 2026 58 / 67
Q1: Can We Create Object of Interface?
Question
Can we create an object of an interface?
interface A { }
A obj = new A();
Answer
No. Interfaces cannot be instantiated.
Premanand S (VIT-CC) Module 1 February 24, 2026 58 / 67
Q2: Can Interface Have Variables?
Question
Is this valid?
interface A {
int x = 10;
}
Premanand S (VIT-CC) Module 1 February 24, 2026 59 / 67
Q2: Can Interface Have Variables?
Question
Is this valid?
interface A {
int x = 10;
}
Answer
Yes. Variables in interface are public static final by default.
Premanand S (VIT-CC) Module 1 February 24, 2026 59 / 67
Q3: Can Interface Have Constructor?
Question
Can an interface have a constructor?
Premanand S (VIT-CC) Module 1 February 24, 2026 60 / 67
Q3: Can Interface Have Constructor?
Question
Can an interface have a constructor?
Answer
No. Interfaces cannot have constructors because objects cannot be
created.
Premanand S (VIT-CC) Module 1 February 24, 2026 60 / 67
Q4: Can Interface Have Static Methods?
Question
Is this valid?
interface A {
static void show() {
[Link]("Hello");
}
}
Premanand S (VIT-CC) Module 1 February 24, 2026 61 / 67
Q4: Can Interface Have Static Methods?
Question
Is this valid?
interface A {
static void show() {
[Link]("Hello");
}
}
Answer
Yes (Java 8+). Static methods are allowed.
Premanand S (VIT-CC) Module 1 February 24, 2026 61 / 67
Q5: Multiple Inheritance
Question
Is this allowed?
interface A { void showA(); }
interface B { void showB(); }
class C implements A, B {
public void showA() { }
public void showB() { }
}
Premanand S (VIT-CC) Module 1 February 24, 2026 62 / 67
Q5: Multiple Inheritance
Question
Is this allowed?
interface A { void showA(); }
interface B { void showB(); }
class C implements A, B {
public void showA() { }
public void showB() { }
}
Answer
Yes. Interface supports multiple inheritance.
Premanand S (VIT-CC) Module 1 February 24, 2026 62 / 67
Q6: Method Without Public
Question
What happens here?
interface A {
void show();
}
class B implements A {
void show() { }
}
Premanand S (VIT-CC) Module 1 February 24, 2026 63 / 67
Q6: Method Without Public
Question
What happens here?
interface A {
void show();
}
class B implements A {
void show() { }
}
Answer
Compilation Error. Interface methods are public by default.
Implementation must be public.
Premanand S (VIT-CC) Module 1 February 24, 2026 63 / 67
Q7: Interface Extending Interface
Question
Is this valid?
interface A {
void show();
}
interface B extends A {
void display();
}
Premanand S (VIT-CC) Module 1 February 24, 2026 64 / 67
Q7: Interface Extending Interface
Question
Is this valid?
interface A {
void show();
}
interface B extends A {
void display();
}
Answer
Yes. An interface can extend another interface.
Premanand S (VIT-CC) Module 1 February 24, 2026 64 / 67
Q8: final Interface?
Question
Can we declare an interface as final?
Premanand S (VIT-CC) Module 1 February 24, 2026 65 / 67
Q8: final Interface?
Question
Can we declare an interface as final?
Answer
No. Interfaces are meant to be implemented. Final prevents inheritance.
Premanand S (VIT-CC) Module 1 February 24, 2026 65 / 67
Q9: Polymorphism with Interface
Question
What is the output?
interface A {
void show();
}
class B implements A {
public void show() {
[Link]("B");
} }
A obj = new B();
[Link]();
Premanand S (VIT-CC) Module 1 February 24, 2026 66 / 67
Q9: Polymorphism with Interface
Question
What is the output?
interface A {
void show();
}
class B implements A {
public void show() {
[Link]("B");
} }
A obj = new B();
[Link]();
Answer
B (Runtime Polymorphism)
Premanand S (VIT-CC) Module 1 February 24, 2026 66 / 67
Thank You!
Stay Connected
Premanand S
Email: premanand.s@[Link]
Phone: +91-7358679961
LinkedIn: [Link]/in/premsanand
Instagram: [Link]/premsanand
WhatsApp Channel: anandsDataX
Google Scholar: Google Scholar Profile
GitHub: [Link]/anandprems
Premanand S (VIT-CC) Module 1 February 24, 2026 67 / 67