Object Oriented Programming(OOP) Explanation

Object Oriented Programming(OOP) Basic Explanation



Object-Oriented Programming (OOP) is a paradigm widely employed in various programming languages such as Java, Python, PHP, JavaScript, C++, and C#. Its fundamental principles revolve around the concept of objects, which can be considered as instances of real-world entities like cars, phones, buses, or pens. In the programming context, objects encapsulate both attributes and methods, collectively defined within a class.

Class and Object:

In OOP, a class serves as a blueprint for creating objects, encapsulating attributes such as model name, color, height, weight, and methods like call(), message(), and browseInternet(). For instance, considering a phone, these attributes and methods collectively constitute the class. When an object is instantiated from this class, it becomes an instance of that specific entity.

we call those things methods. in programming we add these attributes and methods together we call that a CLASS                                                                                                                                       

in this Object Oriented Programming, We can find four main Concept 
 
  1. Encapsulation
  2. inheritance
  3. Abstraction
  4. Polymorphism
i am not going to talk about this with codes i am just going to explain those concepts with some example


Abstraction


Simply abstraction is used to reduce the complexity of the code the other hand if we don't want to show the complexity to the user we use this abstraction concept. in programming, we can use this concept in interfaces or abstract classes.

Let's take a simple example.we created a class called bank A. we created a method getInterrestRate().suppose in this method lot of things happen there for it is very complex code so we dont need to show this to the user. then how we don't show this complexity to the user. we can do that like this. we can create a interface/Abstract class. in this abstract class, we can create methods without implementation. That means there is no content in that interface only there is a definition. like that, we can reduce the complexity of the code.we can call this method in a new object. i show the simple skecth for that.





Encapsulation


Mainly we used this for the security of the class. if we give access to someone to use the attribute and the methods in the class, we use this concept to control the access level.i will explain with a simple example. suppose there is a bank account. actually, you need to know about the access modifiers.
There are few access modifiers (Public, private, protected). If this public modifier is anyone can access the class from any class



if we changed this attribute to private no one can access this attribute from another class.


using getters and setters (with public modifiers) anyone can access those attributes.

Inheritance


Imagine you're designing a family of objects, like cars. You have a basic car with general features like wheels, an engine, and a steering wheel. Now, let's say you want to create a more specific type of car, like a sports car.

Instead of starting from scratch and listing all the basic features again, you can say that a sports car "inherits" from the basic car. This means it automatically gets all the features of a basic car, and you only need to add or modify the things that make it unique as a sports car—like a sleek design or a high-performance engine.

In programming, this is like having a "parent" class (basic car) and a "child" class (sports car). The child class inherits the attributes and behaviors of the parent class and can have its own special characteristics.

So, inheritance in object-oriented programming is a way of creating new classes that are built upon existing classes, reusing their attributes and behaviors, and allowing for customization and extension. It's like saying, "I want a new thing that's a lot like this other thing but with some differences."


There are types of inheritance


Polymorphism


Sure! Think of polymorphism in object-oriented programming (OOP) like this: it's a way for different objects to use the same method name but behave in their own unique way. Imagine you have a TV remote. Regardless of the brand or model, they all have a power button. When you press the power button, each TV responds differently based on its own internal workings. In OOP, polymorphism lets you define a common method (like a power button) in a base class, and then each derived class (like different TV brands) can implement that method in its own way. So, when you call the method on an object, it does what's appropriate for that specific object. It's like saying, "Hey, object, do your thing with this common method," and each object knows how to respond uniquely. That's polymorphism in a nutshell!
you can see another example.
we can achieve this concept from method overloading and method overriding










                                                                                                                                                 

Comments