Skip to main content

OOPS Concepts

✔ There are four major Object-Oriented principles:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

3.2.1 Abstraction

✔ Abstraction is the process of providing necessary properties and operations of an object.

✔ When describing an object, you need to consider various perspectives.


Example: Mobile

To describe a Mobile, you can take different perspectives:

Customer Perspective

  • SMS
  • CALL
  • etc.

Engineer Perspective

  • SOFTWARE
  • HARDWARE
  • etc.

✔ When describing a Mobile for a Customer, we provide only the required properties and operations for the customer by hiding unnecessary details meant for engineers.

👉 This is called Abstraction (hiding unnecessary details)


3.2.2 Encapsulation

✔ Encapsulation is the process of binding (wrapping) state and behavior of an object into a class.


Example

class Person {

int age;
String name;

void walk() {}

void eat() {}
}

✔ Mapping

  • int age, String name 👉 State / Data members / Fields / Properties
  • walk(), eat() 👉 Behavior / Member functions / Methods

💡 Quick Understanding

  • Abstraction → Hide unnecessary details ✔
  • Encapsulation → Bind data + methods ✔
  • Class = container of state + behavior

3.2.3 Inheritance

✔ Inheritance is the process of writing a new class by inheriting commonly used state and behavior of an existing class.


Example Structure

Person (Parent Class)

  • age
  • name
  • walk()
  • eat()

⬇️ Derived into:

Student (Child Class)

  • sid
  • fee
  • read()
  • sleep()

Employee (Child Class)

  • eid
  • salary
  • work()

✔ Existing class is called:

  • Super class / Base class / Parent class

✔ Newly created class is called:

  • Sub class / Derived class / Child class

3.2.4 Polymorphism

✔ Polymorphism = One name, many forms

✔ It is the ability of an object to behave differently in different situations.


Examples

1) Power Button

  • Press once → Device ON
  • Press again → Device OFF

2) + Operator in Java

  • When operands are numeric → performs addition
  • When operands are String → performs concatenation

💡 Quick Understanding

  • Inheritance → code reuse ✔
  • Parent → common properties
  • Child → specialized features
  • Polymorphism → same method/operator, different behavior ✔