Inheritance and Polymorphism

Let's dive deep into Inheritance and Polymorphism, two fundamental pillars of Object-Oriented Programming (OOP) in Java.


🔷 INHERITANCE in Java

📌 Definition:

Inheritance is the mechanism in Java by which one class acquires the properties and behaviors (fields and methods) of another class.

  • The class that inherits is called the subclass (or child class).

  • The class from which it inherits is the superclass (or parent class).


✅ Syntax:


class SuperClass { // fields and methods } class SubClass extends SuperClass { // additional fields and methods }

 Example:


class Animal { void eat() { System.out.println("This animal eats food."); } } class Dog extends Animal { void bark() { System.out.println("The dog barks."); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.eat(); // Inherited method d.bark(); // Own method } }

✅ Output:


This animal eats food. The dog barks.

🔑 Types of Inheritance in Java:

TypeSupported in Java?Example
Single Inheritance        ✅ Yesclass B extends A
Multilevel        ✅ YesA → B → C
Hierarchical        ✅ YesA → B, A → C
Multiple Inheritance        ❌ (via classes) ✅ via interfacesclass C implements A, B

🔷 POLYMORPHISM in Java

📌 Definition:

Polymorphism means “many forms”. In Java, it allows objects to behave differently based on the context, even if they share the same interface or method name.

There are two types:


🔸 1. Compile-time Polymorphism (Method Overloading)

  • Same method name with different parameters (type or number).

  • Resolved at compile time.

✅ Example:


class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator c = new Calculator(); System.out.println(c.add(2, 3)); // 5 System.out.println(c.add(2.5, 3.5)); // 6.0 } }

🔸 2. Runtime Polymorphism (Method Overriding)

  • Same method name and signature in both superclass and subclass.

  • Resolved at runtime using dynamic method dispatch.

✅ Example:

class Animal {
void sound() { System.out.println("Some sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } } class Cat extends Animal { void sound() { System.out.println("Meow"); } } public class Main { public static void main(String[] args) { Animal a; a = new Dog(); a.sound(); // Bark a = new Cat(); a.sound(); // Meow } }

Differences Between Inheritance and Polymorphism

FeatureInheritancePolymorphism
Meaning    Reuse code from parent classSame method behaves differently
Type    Is-a relationshipMany forms
Use    Code reuseFlexibility and dynamic behavior
Achieved byextends keywordOverloading & Overriding

Real-World Analogy

ConceptExample
Inheritance                    A car is a type of vehicle — it inherits basic features like wheels, engine.
Polymorphism                    A remote control works for TV, AC, or fan — the button is the same, but                     the  result differs.

✅ Summary

🔹 Inheritance:

  • Enables code reuse.

  • Subclass inherits fields and methods from the superclass.

🔹 Polymorphism:

  • Enables flexibility.

  • Same method name can behave differently (overloading/overriding).

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming