Understanding OOP -Inheritance, Polymorphism, Encapsulation

 

1. Quick Recap: What is OOP?

OOP (Object-Oriented Programming) is about designing programs around objects that have data (attributes) and behaviors (methods).

Main OOP principles:

  1. Inheritance – Reuse code from one class in another

  2. Polymorphism – One method, many forms

  3. Encapsulation – Protect data by controlling access

  4. Abstraction – Hide implementation details (covered later)


2. Access Modifiers in Java

Access modifiers control visibility of classes, methods, and variables.

ModifierAccess Level
publicAccessible from anywhere
protectedAccessible in same package and subclasses
default (no keyword)Accessible in same package only
privateAccessible only inside the same class

3. Inheritance – Reusing Code

Inheritance allows one class (child/subclass) to get properties and methods from another class (parent/superclass).

Example:

class Vehicle { public void start() { System.out.println("Vehicle is starting..."); } } class Car extends Vehicle { public void honk() { System.out.println("Car is honking..."); } } public class Test { public static void main(String[] args) { Car myCar = new Car(); myCar.start(); // from parent myCar.honk(); // from child } }

Output:

Vehicle is starting... Car is honking...

Types of Inheritance in Java

  • Single – One parent, one child

  • Multilevel – Child → Grandchild → Great-grandchild

  • Hierarchical – Multiple children from one parent

(Java doesn’t support multiple inheritance with classes, but supports it with interfaces.)


4. Method Overriding – Changing Inherited Behavior

If a subclass wants a different version of a parent method, it can override it.

class Animal { public void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override public void sound() { System.out.println("Dog barks"); } } public class Test { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); // Output: Dog barks } }

5. Polymorphism – Many Forms

Polymorphism allows the same method call to behave differently depending on the object.

Example:

class Shape { public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { public void draw() { System.out.println("Drawing a circle"); } } class Square extends Shape { public void draw() { System.out.println("Drawing a square"); } } public class Test { public static void main(String[] args) { Shape s1 = new Circle(); Shape s2 = new Square(); s1.draw(); // Drawing a circle s2.draw(); // Drawing a square } }

6. Encapsulation – Data Protection

Encapsulation means hiding class variables and giving controlled access through getters and setters.

Example:

class BankAccount { private double balance; // private variable public double getBalance() { return balance; } public void deposit(double amount) { if (amount > 0) { balance += amount; } } } public class Test { public static void main(String[] args) { BankAccount acc = new BankAccount(); acc.deposit(500); System.out.println("Balance: " + acc.getBalance()); } }

7. Mini Practice Exercises

Task 1: Create a class Employee with name and salary. Create a subclass Manager that adds a department property and overrides a method displayDetails().

Task 2: Create a superclass Shape and subclasses Circle and Rectangle. Implement area() method in each.

Task 3: Create a Student class with private variables name and marks, and use getters and setters to access them.


8. What’s Next?

In Blog 6, we’ll explore:

  • Arrays (1D, 2D)

  • String handling (String, StringBuilder, StringBuffer)

  • Real examples with arrays and strings


💡 Tip: OOP becomes easier when you relate it to real life think about how cars, people, or phones can be modeled with classes and objects.

Comments

Post a Comment

Popular posts from this blog

Java for Beginners – Control Flow in Java Decisions & Loops

Getting Started with Java – Setup & Your First Program