Introduction to Object-Oriented Programming (OOP)
What Is OOP?
It is a programming style in java in which we try to relate our code with real world examples by creating objects and classes this is what we called as object oriented programming we use this approach to understand the problem in terms of real world scenario so that it becomes easy for us to understand.
Object oriented programming vs procedural programming
Features of object oriented programming:
- It is based on classes and objects
- Data is bound within objects by private/protected keywords.
- It is called as bottom up approach in programming
- High code reusability due to inheritance, interfaces and polymorphism
- Better to relate with real world examples
- Maintenance is easy as everything is based on objects changes in one object does not alter in another object.
- Example language: java, C++, python.
Features of procedural programming:
- It is based on functions no any concept of classes and objects
- Data is global and can be accessed by any function
- It is called as Top down approach in programming
- code reusability is not that much it is only moderate as it can be done by functions
- it can't be relate with real world examples as compared with OOP.
- maintenance is harder because doing change in one function can affect others using shared data
- Example language: c, pascal.
What is class and objects in java ?
OOPS based programming is completely based on classes and objects. A class is not a real world entity, it is a blueprint understood it something like there is a house which itself does not exist but inside a house there are rooms, kitchens, furniture bathroom which are called as objects of that class or like a Bike which it self does not exist but there are different models of bike that exists like splendor, hero, passion, Bullet which all are called as objects of the class Bike for a class there are two things properties and behavior of that class let's see properties and behavior by taking bike as a class.
// Class definition
class Bike {
// Properties (fields) of Bike
String model;
int speed;
// Method to display bike details
void displayInfo() {
System.out.println("Bike Model: " + model);
System.out.println("Top Speed: " + speed + " km/h");
System.out.println("---------------------------");
}
}
public class Main {
public static void main(String[] args) {
// Creating objects for different bike models
Bike hero = new Bike();
hero.model = "Hero";
hero.speed = 100;
Bike splendor = new Bike();
splendor.model = "Splendor";
splendor.speed = 90;
Bike apache = new Bike();
apache.model = "Apache";
apache.speed = 120;
// Displaying details of each bike model
hero.displayInfo();
splendor.displayInfo();
apache.displayInfo();
}
}
Bike Model: Hero
Top Speed: 100 km/h
---------------------------
Bike Model: Splendor
Top Speed: 90 km/h
---------------------------
Bike Model: Apache
Top Speed: 120 km/h
---------------------------
let me explain you so quick what is actually happening in the above code snippet there's a class named Bike and we are creating different objects of it as models like Hero Splendor and Apache you can notice the syntax of creating objects of a class that is important it will be same for all, now what we are actually doing in the above code snippet is we are giving the properties to each object and displaying the information as simple as it is, after giving properties to each object we are calling the display method for each object separately and the same is displaying in the output as you can see in the above output model and speed is displaying for each object hope it makes sense.
Why Use OOP in Java?
we use oops in java because this is a practical approach to solve problems related to coding by the help of real world examples not only java other programming languages use this approach too like python, c++, c#, ruby etc.
Four pillars of OOP in Java
- Encapsulation
- polymorphism
- Inheritance
- Abstraction
let's understand each one of the principal of oops in detail with a very beginner friendly real world example I will make you clear each one of them for you in the easiest way possible I promise just keep reading the article.
Encapsulation: It's a combination of properties and methods (behavior) in a single unit is called encapsulation think of it like there is a medicine capsule inside a capsule it's properties and it's behavior are entitled (bound) to within that capsule only no outside interference, now in terms of java consider it as Laptop is class whose properties and behavior are bound withing that Laptop class only we will see encapsulation in detail with a code example in one separate article so that you won't get confuse for now.
Polymorphism: It simply means many forms for an example there is a person who is a father for his child, son for his parents, husband for his wife, and brother for his sister the person is same but has different forms for different scenarios. In java there are two types of polymorphism mainly compile time (method overloading) and run time polymorphism (method overriding) we will see what they are in detail too you don' have to worry about that.
Type of polymorphism
- Compile time polymorphism - It means when the compiler decides which method to be called at the compile time only example method overloading.
- Run time polymorphism - It means which method to be called is decided at the run time only by the compiler example method overriding.
Inheritance: It simply means inheriting properties and behaviors from parents like a child can inherit some common properties from there parents in terms of look, two hands like that of his parent two legs like that of his parent, one nose, two ear like that of his parents and some behaviors like they can also eat, bath, talk like that of his parents in in the context of java there are different types of inheritance in java mainly 4 types.
Types of inheritance in java:
- Single inheritance
- multiple inheritance (it's not supported directly but we can implement it using interfaces)
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance (it is a combination of two or more types of inheritance achieved through interface)
Abstraction: It means hiding the internal details from the user and showing only the necessary details to the user or outside world like take an example of vehicle there are different vehicles all can start, stop and accelerate but the internal mechanism for each one of them can be different but as a programmer we only care about it's functionality and hides the unimportant details from the user it is called abstraction showing only the required details to the user and hiding internal details it has several advantages in terms of code simplification, reusability and security. In Java there are two methods to achieve abstraction we will see more about that in a separate article with code examples for both the types.
It is very helpful bro
ReplyDeleteGlad you liked our article please share it 😇
Delete