Title Slide
Topic: Inheritance and Polymorphism in Java
Course: Object-Oriented Programming
(OOP) - MCA
Introduction to Inheritance
• Inheritance is a mechanism in Java by which
one class acquires the properties and
behaviors of another class.
• Promotes code reuse.
• Enables hierarchical classification.
Inheritance Hierarchies
• Single Inheritance (one class inherits from one
superclass)
• Multilevel Inheritance (a class is derived from a
class, which is also derived from another class)
• Hierarchical Inheritance (multiple classes
inherit from a single superclass)
• Java does not support multiple inheritance with
classes (to avoid ambiguity)
Superclass and Subclass
Superclass: The class whose features are
inherited.
Subclass: The class that inherits from another
class.
Syntax: class SubclassName extends
SuperclassName { }
A subclass can add its own fields and methods in
addition to the superclass members.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Member Access Rules
• private: Not accessible outside the class.
• default (no modifier): Accessible within the
same package.
• protected: Accessible within the same package
and subclasses.
• public: Accessible from anywhere.
The super Keyword
• Used to refer to the immediate superclass
object.
• super() calls the superclass constructor.
• super.variable and super.method() can access
superclass members.
Example:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
}
}
Preventing Inheritance
• Use the final keyword to prevent inheritance.
• final class: Cannot be subclassed.
• final method: Cannot be overridden by
subclasses.
Example:
final class FinalClass {}
// class Test extends FinalClass { } // Error: cannot inherit
from final class
class Base {
final void display() {
System.out.println("Final method");
}
}
// class Derived extends Base {
// void display() { } // Error: cannot override final method
// }
The Object Class
Every class in Java implicitly inherits from java.lang.Object.
Common methods:
- toString()
- equals(Object obj)
- hashCode()
- getClass()
- clone()
Example:
class Person {
public String toString() {
return "Person class";
}
}
Introduction to Polymorphism
• Polymorphism means "many forms".
• Allows one interface to be used for a general
class of actions.
Two types:
- Compile-time (method overloading)
- Runtime (method overriding)
Dynamic Binding
• Also known as late binding.
• The method call is resolved at
runtime.
• Enables Java to support runtime
polymorphism.
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
Method Overriding
A subclass provides a specific implementation of
a method already defined in its superclass.
Rules:
- Must have the same method name, return
type, and parameters.
- Cannot override final or static methods.
- Use @Override annotation for clarity.
Example:
class Shape {
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing circle");
}
}
Abstract Classes and Methods
• An abstract class cannot be instantiated.
• May contain abstract methods (without
implementation).
• Must be extended and abstract methods must
be implemented in the subclass.
Syntax:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
Summary
• Inheritance enables code reuse and hierarchical
classification.
• The super keyword accesses superclass
members.
• Use final to restrict inheritance.
• Polymorphism allows flexibility in method
calling.
• Abstract classes define templates for future
classes.