There are many types of programming paradigms or styles of programming such as Procedural, Functional, Object-oriented, Event-driven, Logic, Aspect-oriented. Out of these Functional and Object-oriented programming are the most popular programming paradigms. Java supports multiple programming paradigms.

Introduction to OOP

In OOP everything is based on the concepts of objects. These objects contain Data (State) and Methods (Behavior). So in OOP, we keep the data and the behavior together.

Benefits of OOP:

  1. Reduced complexity
  2. Easy maintenance
  3. Code reuse
  4. Faster development

Functional Programming: (Explained later)

In functional programming data and behavior are kept separate.

<aside> 💡 Which is a better programming paradigm? → Choose the programming paradigm which can efficiently solve the problem.

</aside>

It is possible to use multiple paradigms in a single program. So make use of that.

Classes

Class & Objects

A class is a blueprint or a template for creating objects. It is a logical entity. An object is an instance of a class. Any entity that has state and behaviour is known as an object.

For Example,

https://lh7-us.googleusercontent.com/NlVR9gW9GxFKFNIDC8y9WTEyeTJVP3J4_BayKHyuFP8X_ZAzQL0QMORLMVMRXvAnGMUqJnzhwlRGpZOmrzVMiQIP97jwROJ_unRhBqYMKqByyk_R_1ZEA2snpDJewl9yXrTgnFxFtqwKDXD2A04Pb-I

A Car is a class with Data (also called fields) such as started, currentSpeed, currentGear and Methods on the data that change the fields such as start(), stop(), brake(), changeGear(). We can create objects from the class car such as car1, car2, and car3 are objects created from the Car class, but all of them are independent of each other.

Class Creation

In Java, we should add each class to a separate file. Name of the file should be same as that of the class name.

public class TextBox {
   public String text;  // Field 
  
   public void setText(String text) {
       this.text = text;
   }
  
   public void clear() {
       text = "";
   }
}