Inheritance, Polymorphism & Abstraction

Chapter 8: Inheritance, Polymorphism & Abstraction in Java

Chapter 8: Inheritance, Polymorphism & Abstraction in Java

Introduction to Object-Oriented Programming Principles

Java is a pure object-oriented programming language, meaning it is based on the principles of modeling real-world entities as objects. Among the four core pillars of OOP—Encapsulation, Inheritance, Polymorphism, and Abstraction—this chapter dives deep into three essential concepts:

  • Inheritance
  • Polymorphism
  • Abstraction

1. Inheritance in Java

What is Inheritance?

Inheritance is the process where one class (child/subclass) acquires the properties and behaviors (methods and variables) of another class (parent/superclass).

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks.");
    }
}

Types of Inheritance in Java:

  • Single
  • Multilevel
  • Hierarchical

The super Keyword

class Animal {
    String color = "white";
}

class Dog extends Animal {
    String color = "black";
    void printColor() {
        System.out.println(super.color); // prints white
    }
}

2. Polymorphism in Java

What is Polymorphism? Polymorphism means "many forms." It allows a single function name or method to behave differently based on the context.

Types of Polymorphism:

  • Compile-time Polymorphism (Method Overloading)
  • Run-time Polymorphism (Method Overriding)
// Method Overloading
class MathUtils {
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
}

// Method Overriding
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

Dynamic Method Dispatch

Animal obj = new Dog();
obj.sound();  // Output: Dog barks

3. Abstraction in Java

What is Abstraction? Abstraction is the process of hiding internal implementation details and showing only the necessary functionality.

Achieving Abstraction in Java:

  • Abstract Classes
  • Interfaces
// Abstract Class
abstract class Shape {
    abstract void draw();
    void info() {
        System.out.println("This is a shape.");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}
// Interface
interface Printable {
    void print();
}

class Document implements Printable {
    public void print() {
        System.out.println("Printing document...");
    }
}

Abstract Class vs Interface

FeatureAbstract ClassInterface
MethodsAbstract & ConcreteOnly abstract (Java 7)
Multiple InheritanceNot supportedSupported
ConstructorsYesNo
Access ModifiersAll allowedOnly public

Benefits of Using OOP Concepts

  • Code Reusability
  • Scalability
  • Security
  • Modular Design

Chapter Summary

  • Inheritance: Parent-child relationship between classes
  • Polymorphism: Same method behaving differently
  • Abstraction: Hiding complexity, showing functionality

❓ FAQs with Answers:

Q1: What is inheritance in Java?
A: Inheritance is an OOP feature that allows one class (subclass or child) to inherit the properties and behaviors (fields and methods) of another class (superclass or parent). It supports code reusability.

class Animal {
   void eat() { System.out.println("This animal eats food."); }
}
class Dog extends Animal {
   void bark() { System.out.println("Dog barks"); }
}

Q2: What are the types of inheritance supported in Java?
A: Java supports:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Note: Java doesn’t support multiple inheritance with classes to avoid ambiguity, but it does with interfaces.

Q3: What is polymorphism in Java?
A: Polymorphism means "many forms". It allows a single interface to control access to different types of objects. In Java, it comes in two forms:
- Compile-time polymorphism (Method Overloading)
- Run-time polymorphism (Method Overriding)

Q4: What is method overloading and method overriding?
A:
Method Overloading: Same method name with different parameters in the same class.
Method Overriding: Subclass provides a specific implementation of a method declared in the parent class.

Q5: What is abstraction in Java?
A: Abstraction is the process of hiding the internal implementation details and showing only the functionality to the user. It is achieved using:
- Abstract classes
- Interfaces

Q6: What is an abstract class in Java?
A: An abstract class is a class that cannot be instantiated. It may contain abstract methods (without body) and concrete methods.

abstract class Animal {
   abstract void sound();
   void sleep() {
      System.out.println("Sleeping...");
   }
}

Q7: What is an interface in Java?
A: An interface is a completely abstract class used to achieve abstraction and multiple inheritance. All methods are implicitly public and abstract.

interface Animal {
   void sound();
}
class Dog implements Animal {
   public void sound() {
      System.out.println("Dog barks");
   }
}

Q8: Difference between abstract class and interface?

FeatureAbstract ClassInterface
MethodsCan have both abstract and concreteOnly abstract (Java 7)
ConstructorYesNo
Access ModifiersCan have anyPublic only
Multiple InheritanceNot supportedSupported

Q9: What is the use of the super keyword in inheritance?
A: super refers to the parent class. It is used to call parent class constructors, methods,

📌 Related Links: Complete Java Course

Comments

Popular posts from this blog

Best AI Tools for Students and Professionals in 2025 | Boost Learning & Productivity

Introduction to Java

Top AI Tools for Content Creators: Bloggers, YouTubers & Writers (2025)