0% found this document useful (0 votes)
4 views

OOP Introduction Notes Detailed

Object-Oriented Programming (OOP) is a programming paradigm that utilizes objects to encapsulate data and methods, promoting modularity and reusability. Key features include encapsulation, abstraction, inheritance, and polymorphism, which help in modeling complex systems effectively. OOP is essential in modern software development, widely used in languages such as Java, Python, C++, and Ruby.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOP Introduction Notes Detailed

Object-Oriented Programming (OOP) is a programming paradigm that utilizes objects to encapsulate data and methods, promoting modularity and reusability. Key features include encapsulation, abstraction, inheritance, and polymorphism, which help in modeling complex systems effectively. OOP is essential in modern software development, widely used in languages such as Java, Python, C++, and Ruby.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Object-Oriented Programming (OOP) - Introduction

Object-Oriented Programming (OOP) - Introduction

Object-Oriented Programming (OOP) is a programming paradigm


based on the concept of objects, which can represent real-world
entities. These objects encapsulate data (attributes) and methods
(functions), making code modular, reusable, and easier to manage.
OOP is widely used in software development due to its ability to
model complex systems intuitively.

Key Features of OOP:

1. Encapsulation:
Encapsulation is the bundling of data (attributes) and methods
(functions) that operate on the data into a single unit, called a class. It
ensures that the internal details of the object are hidden from the
outside world and can only be accessed through predefined methods.

Example:
class BankAccount {
private double balance;

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
}
}

public double getBalance() {


return balance;
}
}

public class Main {


public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
System.out.println("Balance: " + account.getBalance());
}
}

Output:
Balance: 1000.0

2. Abstraction:
Abstraction hides unnecessary implementation details from the user
and only exposes the essential features. It allows developers to work
with higher-level concepts instead of worrying about the internal
workings.

Example:
abstract class Shape {
abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a Circle");
}
}

public class Main {


public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}

Output:
Drawing a Circle

3. Inheritance:
Inheritance allows a child class to inherit the properties and methods
of a parent class. This promotes code reuse and establishes a
parent-child relationship.

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

class Dog extends Animal {


void bark() {
System.out.println("This dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}

Output:
This animal eats food
This dog barks

4. Polymorphism:
Polymorphism allows an object to take many forms. It is commonly
implemented through method overloading (same method name with
different parameters) and overriding (child class redefines a method
of the parent class).
Example of Overloading:
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 calc = new Calculator();
System.out.println(calc.add(5, 10)); // Output: 15
System.out.println(calc.add(5.5, 10.5)); // Output: 16.0
}
}

Example of Overriding:
class Animal {
void sound() {
System.out.println("This animal makes a sound");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Cat();
animal.sound();
}
}

Output:
Meow

5. Classes and Objects:


A class is a blueprint for objects, and an object is an instance of a
class. Objects have attributes (data) and behaviors (methods).

Example:
class Car {
String brand;
int speed;

void drive() {
System.out.println(brand + " is driving at " + speed + " km/h");
}
}

public class Main {


public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.speed = 120;
car.drive();
}
}

Output:
Toyota is driving at 120 km/h

Advantages of OOP:
1. Modularity: Code is divided into classes, making it easier to debug
and maintain.
2. Reusability: Inheritance and polymorphism enable code reuse.
3. Scalability: New features can be added easily by extending classes.
4. Security: Encapsulation protects sensitive data from external
access.

Conclusion:
OOP provides a powerful way to model real-world systems in
software. By using concepts like encapsulation, inheritance,
polymorphism, and abstraction, developers can build scalable and
maintainable applications. It has become a cornerstone of modern
programming and is widely used in languages like Java, Python, C++,
and Ruby.

You might also like