Object Oriented Programming Concepts

Introduction to OOPS?
The full form of OOPs ( OBJECT ORIENTED PROGRAMMING CONCEPTS ):
- It is a Java programming theory that works based on abstraction, encapsulation, inheritance, and polymorphism.
- It allows users to create objects and create methods to handle those objects.
- The fundamental concept of Object-Oriented Programming is to create objects, reuse them throughout the program, and manipulate these objects to get results.
The OOPS concepts are:
1) Class:
- Class is a group of similar entities.
- It is only the logical component and not the physical existence.
- For example, have a Class “Animals,” it could have objects like ‘Dog,’ ‘Cat,’ ‘Snake,’ etc.
- Properties of these can be color, breed.
- Methods may be performed with this age, name, food.
2) Object:
- It can be defined as an instance of a class, and there can be multiple instances of a class in a program.
- An Object which contains both the data and the method, and operates on the data.
- For example –If the class is Fruit, then the objects are ‘Apple,” Banana,’ ‘kiwi,’ ‘Strawberry,’ etc.
3) Inheritance:
- It is an OOPS concept in which one object can acquire the properties and its behaviors of the parent object.
- Inheritance is creating a parent-child relationship between two classes.
- Inheritance provides a robust and natural mechanism for organizing and structure of any software.
4) Polymorphism:
- Polymorphism can perform a specific action in different ways.
- Polymorphism can achieve by two forms, i.e., Method Overloading and Method Overriding.
- It refers to the ability of a variable, object, or function to take on multiple ways.
- For example, Let’s have a class called ‘Animal’ having a method called ‘animalVoice().’
- So, the derived classes of Animals could be Cats, Dogs, Birds.
5) Abstraction:
- It is an act of representing necessary features without including background details.
- It is a concept to create a new data type for a specific application.
- Let’s have an example that while driving a car, we do not care about its internal working. Here we need to focus on parts like steering wheel, Gears, accelerator, etc.
6) Encapsulation:
- It is a concept of wrapping up the data and code.
- The OOPS concept, the variables of a class are always hidden from other classes.
- It can only be obtained by using the methods of the current Class.
- Example: In a school, a student cannot exist without a class.
7) Association:
- It is a connection between the two objects.
- It defines the variety between objects.
- For example, If there are many students, they can associate with one teacher, or as we say, one student can be associated with multiple teachers for different subjects.
8) Aggregation:
- In this process, all objects have their separate lifecycle.
- However, there is ownership such that child objects do not belong to another parent object.
- Example: Let consider class/objects as department and teacher. Here we can say, a single teacher cannot belong to the multiple departments, but if we want to delete the department of the teacher, the object will never be destroyed.
9) Composition:
- A composition is a generalized form of Aggregation.
- Child objects do not have their lifecycle of their own, so when the parent object deletes, all child objects will also delete automatically.
- For that, let’s take an example of an Apartment and rooms.
- Any apartment can have several rooms.
- Single rooms cannot become part of two different houses. So, if you delete the apartment room will also be deleted.
Pros of OOPS:
- OOPs offers us the easy way to understand and a clear modular structure for programs.
- Objects are created for Object-Oriented Programs that can be reused in other programs. It reduces the development cost.
- The large programs are difficult to write, but if the development and designing team follow the OOPS concept, then they can better design with minimum flaws.
- OOPs also increase program modularity because every object exists independently.
Let’s have Comparison of OOPS with the other programming styles along with the example:
Take an example to show how OOPs is different than other programming approaches.
Programming languages are categorized into three primary types
- Unstructured Programming Languages: In this programming approach, code is repeated again and again throughout the program and has sequential flow control.
- Structured Programming Languages: This approach uses the concepts of function to make code more reusable and has non-sequential flow control.
- Object-Oriented Programming: IT combines Data & Action together.
Let’s take an example here:
Assuming that we want to create a Banking Software with basic functionalities like
- Deposit funds
- Withdraw funds
- Show Account Balance
Unstructured Programming Languages
This Programming language approach will have two variables one for the account number and another for account balance.
int act_number=101;
int act_balance=1000;
Suppose we want to deposit 50 dollars.
act_balance=act_balance+50
Then we want to display account balance.
printf(“Account Number=%d,act_number)
printf(“Account Balance=%d,act_balance)
Now we want to withdraw 20 dollars.
act_balance=act_balance-20
Again, to display the account balance, the code will be.
printf(“Account Number=%d,act_number)
printf(“Account Balance=%d,act_balance)
Now to deposit or withdrawal further, you will repeat the code.
Structured Programming
In the structured programming approach, repeated lines on the code were put into some structures such as functions or methods. And whenever needed, we call a function.
Object-Oriented Programming
In this approach, we deal with data or perform specific operations on the data. In this approach combining the Data and Operations is done. Therefore, this approach is called OOPS.
Class Account{
int act_number;
int act_balance;
public void showdata(){
system.out.println(“Account Number”+act_number)
system.outprintln(“Account Balance”+ act_balance)
}
}
12 Comments