Object Oriented Programming
A programming paradigm
Introduction
Object-Oriented Programming (OOP) is a powerful and widely adopted programming paradigm that has revolutionized the way software is designed, developed, and maintained. It offers a structured approach to organizing code by representing real-world entities and their interactions using objects, classes, and principles like encapsulation, inheritance, abstraction, and polymorphism. OOP’s design philosophy centres around modularity, reusability, and maintainability, making it an essential tool for modern software development.
Object-Oriented Programming Concepts
- Class
- Object
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
Class
It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties.
// Create an object "myObj" and print the value of x
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Object
Objects are called instances of a class which are created from a class in any language. They have states and behaviour. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. Each object contains data and codes to manipulate the data. Objects can interact without having to know details of each other’s data or code, it is sufficient to know the type of message accepted and the type of response returned by the objects.
Abstraction
Abstraction is the process of simplifying complex reality by modelling classes based on essential features and ignoring irrelevant details. Abstraction allows developers to create high-level descriptions of objects and their interactions without delving into implementation specifics. This aids in managing the complexity of large software systems, as developers can focus on designing the interactions between objects rather than the intricacies of their implementations. Abstract classes and interfaces provide a way to define a contract for derived classes, ensuring consistent behaviour across different implementations.
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Inheritance
Inheritance, another fundamental OOP principle, allows new classes (subclasses) to inherit properties and behaviours from existing classes (superclasses). This promotes code reuse and enables the creation of specialized classes that build upon the functionality of their parent classes. Inheritance also supports the “is-a” relationship, where a subclass is considered to be a specific type of its parent class. This facilitates the hierarchical classification of objects and promotes a clear and intuitive class hierarchy.
- subclass (child) — the class that inherits from another class
- superclass (parent) — the class being inherited from
To inherit from a class, use the extends
keyword.
//create a superclass
Class Add {
int my;
int by;
void setmyby (int xy, int hy) {
my=xy;
by=hy;
}
}
// create a sub class
class b extends add {
int total;
void sum ();
public Static void main (String args[]) {
b subOb= new b ( );
subOb. Setmyby (10, 12);
subOb. Sum ( ) ;
System.out.println(“total = ” + subOb. Total);
}
}
Polymorphism
Polymorphism is the ability of different classes to be treated as instances of a common superclass. It enables the same method to produce different results depending on the actual class of the object. This flexibility allows for the implementation of dynamic behaviour at runtime, enhancing the adaptability and extensibility of the code. Polymorphism encourages a design principle called “program to an interface, not an implementation,” promoting flexibility and reducing dependencies between classes.
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
Encapsulation
Encapsulation is a key principle that encapsulates these attributes and methods within a class, allowing controlled access to the object’s internal state. This promotes data hiding, preventing direct manipulation of the object’s internal data from outside the class, enhancing security and reducing unintended side effects. For example, a capsule is wrapped with different medicines.
Encapsulation also leads to data abstraction or data hiding. Using encapsulation also hides the data.
- declare class variables/attributes as
private
- provide public get and set methods to access and update the value of a
private
variable - provide public get and set methods to access and update the value of a
private
variable
Advantages of Object-Oriented Programming
Modularity and Reusability:
OOP encourages the creation of modular and reusable components. Well-designed classes can be reused in multiple projects, reducing development time and effort. The modular design also makes it easier to manage and update code.
Code Organization:
OOP promotes a clear code organisation by grouping related properties and methods within classes. This structure improves code readability and maintainability, making it easier to understand and work on complex systems.
Collaboration:
OOP encourages team collaboration by providing clear interfaces and defined responsibilities for each class. Different team members can work on different classes simultaneously, reducing conflicts and facilitating parallel development.
Real-World Modeling:
OOP enables developers to model real-world entities, relationships, and interactions more accurately. This makes the codebase easier to understand for both developers and domain experts.
Design Patterns:
OOP design patterns are reusable solutions to common programming problems. They provide proven strategies for structuring code and solving specific design challenges, promoting best practices in software development.
Graphical User Interfaces (GUIs):
OOP is commonly used to develop graphical user interfaces. GUI frameworks often involve creating classes representing UI elements, such as windows, buttons, and text fields, and defining their behaviour through methods.
Software Maintenance:
OOP’s modular structure and encapsulation make it easier to identify and isolate issues during software maintenance. Changes can be made to individual classes without affecting the entire application.
Conclusion
In conclusion, Object-Oriented Programming (OOP) is a programming paradigm that has transformed the way software is designed and developed. By modelling real-world entities using classes, objects, and principles such as encapsulation, inheritance, abstraction, and polymorphism, OOP provides a powerful and structured approach to software development. Its emphasis on modularity, reusability, and maintainability makes it an essential tool for building complex and scalable software systems. OOP’s ability to represent complex systems in a comprehensible manner and its support for code organization and collaboration has cemented its place as a cornerstone of modern programming practices.