0% found this document useful (0 votes)
10 views7 pages

OOPs Crash Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

OOPs Crash Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object-Oriented Programming (OOPs) Notes

1. Introduction to OOPs

• Definition:
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
“objects”, which contain data (attributes) and functions (methods).

• Objective:
To design programs that are modular, reusable, and easy to maintain.

• Key Concept:
OOP models real-world entities as objects. For example, a Car object may have attributes like
color, model, and methods like start(), stop().

• Comparison with Procedural Programming:

Feature Procedural Programming OOPs

Focus Functions/Procedures Objects/Classes

Data Global or local Encapsulated within objects

Reusability Low High (via inheritance and polymorphism)

Maintenance Difficult Easier

2. Basic Concepts of OOPs

There are 4 main pillars of OOPs:

2.1 Class

• Definition: A class is a blueprint or template for creating objects.

• Syntax (Java Example):

class Car {

String color;

String model;

void start() {

System.out.println("Car started");

• Explanation:
Car is a class with attributes color and model, and a method start().
2.2 Object

• Definition: An object is an instance of a class.

• Syntax:

Car myCar = new Car(); // myCar is an object of Car class

• Key Point: Each object has its own copy of attributes.

2.3 Encapsulation

• Definition: Wrapping data (attributes) and methods into a single unit (class) and controlling
access using access modifiers.

• Access Modifiers:

o private → accessible only inside class

o public → accessible from anywhere

o protected → accessible within package and subclasses

• Example:

class Student {

private int marks; // Encapsulated

public void setMarks(int m) {

marks = m;

public int getMarks() {

return marks;

• Advantage: Data security and controlled access.

2.4 Inheritance

• Definition: Mechanism by which a class (child/subclass) inherits properties and methods of


another class (parent/superclass).

• Types:

o Single Inheritance
o Multilevel Inheritance

o Hierarchical Inheritance

o Multiple Inheritance (in some languages like C++, not Java)

• Example:

class Vehicle {

void fuel() {

System.out.println("Vehicle fueled");

class Car extends Vehicle {

void drive() {

System.out.println("Car is driving");

Car c = new Car();

c.fuel(); // inherited from Vehicle

c.drive();

2.5 Polymorphism

• Definition: Ability of a method, object, or operator to take multiple forms.

• Types:

1. Compile-Time (Method Overloading)

▪ Same method name, different parameters.

class MathOperation {

int add(int a, int b) { return a+b; }

double add(double a, double b) { return a+b; }

2. Run-Time (Method Overriding)

o Subclass provides its own implementation of a superclass method.


class Animal {

void sound() { System.out.println("Some sound"); }

class Dog extends Animal {

void sound() { System.out.println("Bark"); }

2.6 Abstraction

• Definition: Hiding implementation details and showing only the essential features.

• Achieved By:

o Abstract Classes

o Interfaces

• Example (Java Abstract Class):

abstract class Shape {

abstract void draw(); // abstract method

class Circle extends Shape {

void draw() {

System.out.println("Circle drawn");

3. Key OOPs Features

3.1 Constructors

• Definition: Special method to initialize objects.

• Example:

class Student {

String name;

Student(String n) { // constructor

name = n;

}
}

Student s = new Student("Sumathi");

3.2 this Keyword

• Refers to the current object.

class Car {

String model;

Car(String model) {

this.model = model; // differentiates local and class variable

3.3 Static Members

• Shared across all objects of a class.

class Counter {

static int count = 0;

Counter() { count++; }

3.4 final Keyword

• Prevents modification:

o final variable → constant

o final method → cannot override

o final class → cannot inherit

4. Object Relationships

• Association: One class uses another.

• Aggregation: “Has-a” relationship, part can exist independently.

• Composition: “Has-a” relationship, part cannot exist independently.

• Example:

class Engine {}

class Car {

private Engine engine; // composition

Car() { engine = new Engine(); }


}

5. Advantages of OOPs

1. Reusability: Classes and objects can be reused.

2. Scalability: Easy to add new features.

3. Maintainability: Bugs are easier to locate and fix.

4. Real-World Modeling: Closer to how we think in the real world.

5. Data Security: Encapsulation hides data.

6. Comparison of OOPs Languages

Feature Java C++ Python

Object-Oriented Yes Yes Yes

Multiple Inheritance No (interface) Yes Yes

Platform Independence Yes No Yes

Memory Management Automatic Manual Automatic

7. Common OOPs Design Principles

• SOLID Principles:

1. S: Single Responsibility Principle

2. O: Open/Closed Principle

3. L: Liskov Substitution Principle

4. I: Interface Segregation Principle

5. D: Dependency Inversion Principle

• DRY Principle: Don’t Repeat Yourself.

• KISS Principle: Keep It Simple, Stupid.

8. UML in OOPs

• UML (Unified Modeling Language): Graphical representation of classes and objects.

• Components:

1. Class Diagram
2. Object Diagram

3. Sequence Diagram

• Example Class Diagram:

-----------------

| Car |

-----------------

| model: String |

| color: String |

-----------------

| start() |

| stop() |

-----------------

9. Common OOPs Mistakes

1. Overusing inheritance instead of composition.

2. Ignoring encapsulation and making all fields public.

3. Poor method naming.

4. Not using polymorphism correctly.

5. Excessive coupling between classes.

10. Conclusion

• OOPs is a powerful paradigm for building structured and maintainable software.

• By understanding its pillars (Encapsulation, Inheritance, Polymorphism, Abstraction),


developers can write reusable, secure, and scalable code.

• Modern languages like Java, C++, Python, and C# support OOPs principles fully.

You might also like