top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

Object-Oriented Programming (OOP)

Introduction

What is object oriented programming OOP? At its core, OOP revolves around the concept of objects. An object represents a real-world entity or an abstract concept and consists of attributes (data) and methods (behavior). 

For example, a "Car" object can have attributes like color, brand, and speed, as well as methods like acceleration and brake.

OOP is a popular programming paradigm that organizes code into reusable and modular objects. It emphasizes the concepts of objects, classes, and interactions, enabling developers to create efficient, maintainable, and scalable software solutions. In this article, we will delve into the core principles, benefits, limitations, and applications of OOP, focusing on Java.

Overview

OOP provides a structured approach to software development by breaking down complex systems into manageable entities called objects. These objects encapsulate both data and behavior, allowing for easy manipulation and interaction. 

This paradigm encourages the reuse of code, promotes code organization, and enhances the overall productivity of developers. Let’s find out the answer to what is object-oriented programming in Java.

What is Object Oriented Programming OOP in Java?

Java is a widely used programming language that fully embraces the principles of OOP. It provides a rich set of features, such as classes, objects, inheritance, and polymorphism, making it an excellent choice for OOP development. 

Let's take a closer look at some key OOP concepts in Java.

Pillars of OOP

OOP is built upon four fundamental pillars: Abstraction, Encapsulation, Inheritance, and Polymorphism. Let's explore each of these pillars with object-oriented programming examples.

Abstraction

Abstraction allows the creation of abstract classes and interfaces to define common behaviors and characteristics without providing implementation details. It focuses on what an object can do rather than how it does it. Abstract classes cannot be instantiated, and their methods need to be implemented by derived classes.

Output:

In the example above, the "Shape" class is an abstract class that defines an abstract method called "draw()". The "Circle" and "Square" classes inherit from the "Shape" class and provide their implementations of the "draw()" method. 

In the main method, we create objects of the derived classes but refer to them using the base class type. This allows us to treat different shapes uniformly and invoke the appropriate version of the "draw()" method.

Encapsulation

Encapsulation refers to the bundling of data and methods within a class, allowing access to them through well-defined interfaces. It protects data from direct manipulation and provides control over data modification. Access specifiers like public, private, and protected are used to define the visibility of members.

Output:

The "BankAccount" class encapsulates the account number and balance as private data members in the example above. The methods "deposit()", "withdraw()", and "getBalance()" provide the necessary interface to manipulate and access the data. 

By encapsulating the data, we ensure that it is accessed and modified only through the defined methods, ensuring data integrity and security.

Inheritance

Inheritance is a fundamental concept in OOP that allows the creation of new classes (derived classes) based on existing classes (base or parent classes). It facilitates code reuse, promotes modularity, and enables the creation of specialized classes. 

For example, a "SportsCar" class can inherit attributes and methods from the base "Car" class while adding its own unique features.

Output:

The example demonstrates inheritance by creating a "SportsCar" class that extends the "Car" class. The "SportsCar" class inherits the attributes and methods from the "Car" class, including "color", "brand", "accelerate()", and "brake()". 

Additionally, the "SportsCar" class introduces a new method called "turboBoost()".

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common parent class. It enables flexibility and code extensibility. Polymorphism is achieved through method overriding and method overloading.

  • Method Overriding

Method overriding occurs when a derived class provides its implementation of a method that is already defined in the base class. The method in the derived class should have the same name, return type, and parameters as the base class method. 

Output:

In the example above, we define a base class called "Vehicle" with a method called "move()". The "Car" and "Bike" classes inherit from the "Vehicle" class and override the "move()" method with their specific implementations. 

In the main method, we create objects of the base class and derived classes and invoke the "move()" method. The appropriate overridden version of the method is called based on the actual object type.

  • Method Overloading:

Method overloading allows the definition of multiple methods with the same name but different parameters within a class. The compiler determines the appropriate method to invoke based on the arguments provided during the method call.

Output:

In the example above, the "Calculator" class defines two "add()" methods. One accepts two integer parameters, and the other accepts two double parameters.

The appropriate method is called based on the argument types provided during the method call.

OOPs Concepts: Basics to understand What Is Object Oriented Programming Oop Javatpoint

Apart from the pillars of OOP, several other concepts contribute to its principles and practice. Let's explore them briefly.

Object

An object is an instance of a class that represents a specific entity or concept. It encapsulates data (attributes) and behavior (methods). Objects are the building blocks of an application, and interactions between objects drive its functionality.

Class

A class is a blueprint or template that defines the structure and behavior of objects. It specifies the attributes and methods that objects of that class will possess. Objects are created based on the class, and each object can have its specific data while sharing the common behavior defined by the class.

Coupling

Coupling refers to the degree of dependency between classes or modules. Low coupling promotes modular design and makes the system more flexible, maintainable, and scalable. It is achieved by minimizing interdependencies and creating loosely coupled components.

Cohesion

Cohesion refers to the degree to which the elements of a module or class are related and work together to achieve a common purpose. High cohesion indicates that the elements within a module or class are closely related and focused on a single responsibility. It enhances code readability, reusability, and maintainability.

Composition

Composition is a way to build complex objects by combining simpler objects or components. It represents a "has-a" relationship between objects, where one object is composed of one or more other objects. Composition allows for the creation of complex structures while maintaining flexibility and modularity.

Association

Association represents a relationship between two or more objects. It can be a "has-a" or "uses-a" relationship. Objects may be associated with each other to achieve specific functionality. For example, a "Teacher" class may be associated with multiple "Student" objects.

Aggregation

Aggregation is a specific form of association where one object is considered a whole and consists of parts. It represents a "has-a" relationship, but the parts can exist independently of the whole. For example, a "University" object can be composed of multiple "Department" objects.

Why Should We Use OOP?

There are several reasons to embrace OOP in software development:

1. Code Reusability: OOP promotes code reusability by creating objects and classes that can be reused in different parts of an application or even in different projects.

2. Modularity: OOP allows developers to break down complex systems into manageable modules or classes, making it easier to understand, maintain, and extend the codebase.

3. Scalability: OOP provides a scalable approach to software development by allowing the addition of new classes and objects without affecting existing code, facilitating the growth of the application.

4. Code Organization: OOP promotes better code organization through encapsulation, abstraction, and other principles, leading to cleaner and more maintainable codebases.

Where Is OOP Used?

OOP is used in a wide range of applications, including:

1. Software Development: Desktop applications, web applications, mobile applications, and business systems all make considerable use of OOP. OOP aspects are supported by languages like Java, C++, C#, and Python.

2. Game Development: The modeling of game objects, interactions, and behaviors primarily rely on OOP ideas. Game engines like Unity and Unreal Engine support OOP.

3. Object-Oriented Database Systems: Object-oriented database systems store and manage data using the principles of OOP. They enable the persistence of objects and provide rich query capabilities.

Limitations of OOP

While OOP has numerous advantages, it also has some limitations:

1. Learning Curve: For beginners, OOP can be challenging since it requires changing how they think about and comprehend abstract ideas.

2. Performance Overhead: OOP systems may have a modest performance overhead due to features like dynamic dispatch and object indirection.

3. Excessive Inheritance: Excessive inheritance can result in complicated class hierarchies, sometimes known as the "diamond problem" or inheritance hierarchy explosion, which makes it difficult to maintain and comprehend the code.

What Is Object-Oriented Programming OOP in C++?

Object-oriented programming (OOP) in C++ is a programming paradigm that allows you to create objects with data and associated behaviors. It emphasizes the concept of classes and objects, enabling you to model real-world entities and their interactions. 

Let's explore the basic concept of OOP in C++ with a simple example.

In C++, classes serve as blueprints for creating objects. They define the attributes (data members) and behaviors (member functions) that objects of that class will possess. The class acts as a user-defined data type, providing a structure to organize related data and functions.

Here's an example that demonstrates the basic concept of OOP in C++:

Output

In the above example, we define a class called `Person` that represents a person with attributes `name` and `age`. The class has a constructor to initialize the object's state when created. Additionally, it has a member function called `displayInfo()` that prints the person's name and age.

In the `main()` function, we create two objects of the `Person` class, `person1` and `person2`, by calling the constructor and passing the corresponding values. We then call the `displayInfo()` member function on each object to display their information.

This example illustrates the basic principles of OOP in C++. The class serves as a blueprint for creating objects, and objects encapsulate data and behavior within themselves.

Conclusion

In this comprehensive article, we explored object oriented programming oop with example. OOP is a powerful programming paradigm that enables the creation of efficient, modular, and scalable software solutions. 

By understanding the core principles, concepts, and benefits of OOP, developers can leverage its capabilities to build robust and maintainable applications across various domains.

FAQ

1. How are Java abstract classes and interfaces different?

 Java abstract classes cannot be instantiated and have abstract and non-abstract methods. It offers basic functionality for other classes. Java interfaces are contracts that specify a class's methods. It solely contains abstract method signatures, constants, and static methods. A class can implement several interfaces but only one abstract class.

2. How does Java handle exceptions?

   Java exception handling lets you handle runtime faults and extraordinary conditions. Try-catch blocks catch and elegantly manage exceptions. The try block contains code that may throw an exception, while the catch block specifies the type of exception and the code to execute. Java's final block executes regardless of exceptions.

3. What distinguishes Java classes and objects?

   Java classes specify the properties (data members) and behaviors (methods) of their objects. User-defined data type. Objects are class instances. It represents a class-based entity. Objects have class-defined operations and unique data. They call methods and share data.

Leave a Reply

Your email address will not be published. Required fields are marked *