Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

OBJECT-ORIENTED

PROGRAMMING
CHAPTER 4

BY THEISA
Understanding the principles of Object-Oriented Programming
(OOP) is crucial for developing efficient, maintainable, and scalable
software applications. OOP principles promote code reusability,
encapsulation, modularity, inheritance, polymorphism, abstraction,
and good programming practices. It also facilitates collaboration,
improves code understanding, and supports software design
patterns. By using OOP, developers can write code that is easier to
maintain, understand, and modify, leading to higher quality
software applications.
ABSTRACTION
Abstraction is the process of focusing on essential features of an object, while ignoring
irrelevant details. In programming, abstraction involves creating models of complex
systems that include only the essential features needed to understand and use them.

Abstraction is important in programming because it helps to simplify complex systems,


reduce the amount of code needed to represent them, and make them easier to
understand and modify. By focusing only on essential features, abstraction also reduces
the risk of errors, improves code quality, and speeds up development time.

Examples of Abstraction in programming include:


 A car dashboard that displays only the essential information needed to operate the
vehicle, such as speed, fuel level, and engine temperature.
 A video game that simulates a complex world, but only displays a small portion of it at
any given time, and only renders objects that are visible to the player.
 A database that abstracts the details of storage and retrieval, allowing users to work
with data at a higher level of abstraction, such as tables and records.
Abstraction can be achieved in programming through techniques such as
encapsulation, inheritance, and interface design. Encapsulation involves
hiding the internal workings of an object from other parts of the
application, while inheritance allows developers to create new objects
based on existing ones, inheriting their properties and behaviors.
Interface design involves defining a set of abstract methods that must be
implemented by any object that uses the interface, allowing different
objects to be used interchangeably in the same context.
#include <iostream>

SYNTAX :- // Abstract class


class AbstractClass {
public:
// Pure virtual function (abstract method)
virtual void abstractMethod() = 0;

// Normal member function


void normalMethod() {
std::cout << "This is a normal method." << std::endl;
}
};
// Derived class
class DerivedClass : public AbstractClass {
public:
void abstractMethod() {
std::cout << "Implementation specific to the derived class." << std::endl;
}
};

int main() {
// Create an object of the derived class
DerivedClass derivedObj;

// Call the abstract method and the normal method


derivedObj.abstractMethod();
derivedObj.normalMethod();

return 0;
}
ENCAPSULATION

Encapsulation is a fundamental concept in object-oriented programming that refers to the practice


of bundling data and methods that operate on that data within a single unit, such as a class.

Encapsulation is important in programming because it allows the developer to create modular and
organized code that is easier to maintain, debug, and update. By hiding the internal details of an
object's implementation, encapsulation also makes it easier to change the implementation
without affecting the rest of the code.

Examples of encapsulation in programming include the use of access modifiers, such as private
and public, to control access to an object's properties and methods, and the creation of getters
and setters to allow controlled access to an object's state.

Encapsulation can be achieved in programming by defining classes with private data members and
public methods that provide controlled access to those members. By encapsulating data within an
object, the developer can ensure that it is only modified in a controlled and consistent manner,
reducing the risk of bugs and improving code quality.
class MyClass {
private:
SYNTAX :- // Private data members
int privateData;

public:
// Public member functions

// Setter method
void setPrivateData(int newData) {
privateData = newData;
}

// Getter method
int getPrivateData() {
return privateData;
}

// Other member functions...


};

int main() {
// Create an object of the class
MyClass obj;

// Set and get the private data


obj.setPrivateData(10);
int data = obj.getPrivateData();

return 0;
}
INHERITANCE

Inheritance is another fundamental concept in object-oriented programming that refers to the


practice of creating new classes that inherit properties and methods from existing classes. The
existing class is known as the superclass or parent class, and the new class is known as the
subclass or child class.

Inheritance is important in programming because it allows the developer to create classes that are
based on existing classes, inheriting their properties and methods while adding new functionality.
This makes it easier to write and maintain code, as well as to create more complex and specialized
classes.

Examples of inheritance in programming include creating a subclass of a vehicle class that inherits
properties such as color, weight, and number of wheels, while adding new methods and properties
specific to a car or a motorcycle.

Inheritance can be achieved in programming by using the extends keyword to create a subclass
that inherits from a parent class. The subclass can then override methods and properties from the
parent class, or add new methods and properties as needed. By using inheritance, the developer
can create a hierarchy of classes that share common properties and methods, making the code
more organized and easier to maintain.
// Base class
SYNTAX :- class BaseClass {
protected:
// Protected members

public:
// Public members

// Constructor
BaseClass() {
// Constructor implementation
}

// Destructor
~BaseClass() {
// Destructor implementation
}

// Member functions
void baseMethod() {
// Base class method implementation
}
};

// Derived class
class DerivedClass : public BaseClass {
private:
// Private members
Continue ……
public:
// Public members
SYNTAX :- // Constructor
DerivedClass() : BaseClass() {
// Derived class constructor implementation
}

// Destructor
~DerivedClass() {
// Derived class destructor implementation
}

// Member functions
void derivedMethod() {
// Derived class method implementation
}
};

int main() {
// Create an object of the derived class
DerivedClass derivedObj;

// Access base class member function


derivedObj.baseMethod();

// Access derived class member function


derivedObj.derivedMethod();

return 0;
}
POLYMORPHISM
Polymorphism is a feature in object-oriented programming that allows objects of different classes to
be treated as if they were of the same class. It means that a single method can have different
implementations in different classes, and it can be called using the same interface.

Polymorphism is important because it allows for more flexibility and extensibility in the code. It
enables developers to write code that can handle different types of objects without knowing their
exact type at compile time. This reduces code duplication, improves code organization, and makes the
code more modular and easier to maintain.

A common example of Polymorphism is the use of the "+" operator in different contexts. For example,
the "+" operator can be used to add two integers, concatenate two strings, or combine two arrays. In
each case, the operation is performed in a different way, but the same operator can be used to invoke
it.

Another example is the implementation of a Shape class hierarchy. Each subclass of the Shape class,
such as Circle, Rectangle, and Triangle, can implement the draw() method differently. However, a client
code can invoke the draw() method on any Shape object, without knowing its exact type, and the
correct implementation will be called automatically.
Polymorphism can be achieved in programming by using inheritance and
method overriding. Inheritance allows a subclass to inherit the attributes
and methods of its superclass, and method overriding allows a subclass to
provide its own implementation of a method inherited from its superclass.
When a method is called on an object, the implementation that is executed
depends on the actual type of the object, not just its declared type. This
allows different objects of different classes to be treated as if they were of
the same class, as long as they share a common interface.
// Base class
class BaseClass {

SYNTAX :- public:
// Virtual function
virtual void polymorphicMethod() {
// Base class implementation
}
};

// Derived class 1
class DerivedClass1 : public BaseClass {
public:
// Override the virtual function
void polymorphicMethod() override {
// Derived class 1 implementation
}
};
// Derived class 2
class DerivedClass2 : public BaseClass {
public:
// Override the virtual function
void polymorphicMethod() override {
// Derived class 2 implementation
}
};
int main() {
// Create objects of different classes
BaseClass* baseObj1 = new DerivedClass1();
BaseClass* baseObj2 = new DerivedClass2();

// Call the polymorphic method on the objects


baseObj1->polymorphicMethod();
baseObj2->polymorphicMethod();

// Clean up the objects


delete baseObj1;
delete baseObj2;

return 0;
}
SUMMARY
Encapsulation: The practice of hiding the internal details of an object from the outside world.

Abstraction: The practice of representing complex real-world objects as simpler models in code.

Inheritance: The ability of a subclass to inherit properties and behavior from its superclass.

Polymorphism: The ability of different objects to be treated as if they were of the same class.

Importance of understanding and applying OOP principles in programming:


Understanding and applying OOP principles is important because it can make the code more
modular, flexible, and easier to maintain. OOP principles promote code reuse, reduce code
duplication, and improve code organization. Additionally, OOP is a widely used paradigm in the
software industry, and understanding OOP principles can help developers to work collaboratively
and write code that is easier for others to understand.
REFERENCES:
Resources for learning about OOP principles:

Oracle. (n.d.). The Java Tutorials - Object-Oriented Programming Concepts.


Retrieved from https://docs.oracle.com/javase/tutorial/java/concepts/index.html

Microsoft. (n.d.). Object-Oriented Programming Concepts. Retrieved from


https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/

GeeksforGeeks. (n.d.). Object Oriented Programming (OOPs) Concept in Java.


Retrieved from
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-jav
a
/

Udacity. (n.d.). Object-Oriented JavaScript. Retrieved from


https://www.udacity.com/course/object-oriented-javascript--ud711

Khan Academy. (n.d.). Object-oriented design. Retrieved from https://


www.khanacademy.org/computing/computer-programming/programming#oop-k1
2

You might also like