Professional Documents
Culture Documents
Chapter 4
Chapter 4
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.
int main() {
// Create an object of the derived class
DerivedClass derivedObj;
return 0;
}
ENCAPSULATION
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;
}
int main() {
// Create an object of the class
MyClass obj;
return 0;
}
INHERITANCE
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;
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();
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.