Object-oriented programming (OOP)
Object-oriented programming or OOP is a commonly used programming paradigm.
Let’s take a closer look at it. In early programs a list of step by step instructions are common. While this approach, called structural or procedural, was good enough to write a simple applications, with growing complexity and size of the source code, there was a need for something more convenient. And that’s where Object Oriented Programming, or simply OOP, steps in.
OOP solves a problem by creating objects that interact with each other. An object can be anything, a dog, a game player, or a UI element such as a button. It describes what a thing is and what it can do. It is like a blueprint or template were you define the attributes and method inside a class..
For example, a class of Dog has an attributes of breed, gender and age, and it also has methods or functions like eat, bark, and sleep.
class Dog{
String breed;
String gender;
int age;
public void eat(){
System.out.println("Eating");
}
public void sleep(){
System.out.println("Sleeping");
}
}
In OOP we define attributes and methods inside a class. What’s great about OOP is that you can create as many instances as you want without the need to implement functions for each of them.
For instance, If I want to create an object of the class dog, I will do it as written below.
Dog dog1 = new Dog();
dog1.breed = "Bulldog";
dog1.gender = "Male";
dog1.age = 2;
Dog dog2 = new Dog();
dog1.breed = "Golden Retriever";
dog1.gender = "Female";
dog1.age = 4;
That is to say, Object Oriented Programming or OOP allows you to write code with none or minimal code duplication. As a result, this helps keep programs organized and working with them easier.