Fundamentals of object-oriented programming in Java

Today, we will learn the fundamentals of Object Oriented programming programming. First of all, we have to know the meaning of Object and then we go through the others part.

so,
Java is an object-oriented programming language.
It is based on the concept of objects
Object-Oriented Programming is a programming paradigm where everything is represented as objects.

It mainly focuses on the states of the object and the behaviors.

Objects pass messages to each other.
Each object decides what to do with a received message.

These objects are thought to have two characters:

  1.     State- (fields)
  2.     Behaviors- (methods)

Today we will know, what is the object? What is class? How do these work?

We can imagine everything around us as an object. For example, I have a bicycle. Then this bicycle is an object. Why did I call it an object? Because it has some states and behaviors.

For example, it has two wheels, a hood, has one or more gears, etc. It also starts moving when pedaled, stops when braked, which is why we can call it an object.

So we can define the object as follows:
    An object is an entity that has states and behaviors.

I can say it like this again
    An object is called an instance of a class

Thus, with the state we can understand what the object might be or how it might be.
State tells us how the object looks orwhat properties it has.

And from the behavior we know what that object can do is its action.
    Behavior tells us what the object does.

I got to know the object but what is the class again? Hmmm, let’s see if this is the class

To understand the class, we can take a fanny example, for example, in rural Bengal, a kind of mold cake is made. Where rice water-powder is mixed in a clay or iron mold and put in each hole of the mold, one cake is made. This way many cakes can be made very easily.

 Just like the class and the mold. This is called the blueprint for creating objects. That is, a class can have more than one object and this is the main advantage of Object Oriented.

  So what is class?

    A class is a template or blueprint from which objects are created.

So we can define state as instance variable and behavior as instance method.

So, Classes define states as instance variables and behaviors as instance methods.

  1. Instance variables are often called member variables.
  2. Instance variables are also known as member variables.

So to better understand the class and the object, let us look at the following example,
First let’s create a class called Car.

Car.java

package com.oop;

public class Car {

    // instance variable    public String carName;
    public int numOfWheel;
    public String color;



    // method    public void start(){
        System.out.println("Car is staring...");
    }

    // method    public void stop(){
        System.out.println("Car is stoping...");
    }

    // default override method    @Override    public String toString() {
        return "Car{" +
                "carName='" + carName + '\'' +
                ", numOfWheel=" + numOfWheel +
                ", color='" + color + '\'' +
                '}';
    }
}

In the middle of the class, I declare three instant variables named name, color, number of wheels.

These are:

  1. Since carName is a string, I have given its data type string.
  2. numOfWheel means the number of wheels so I have given its data type int (integer).
  3. color is a string, so I have given a data type string.

// instance variablepublic String carName;
public int numOfWheel;
public String color;
And one thing to note is that we have made each variable public so that we can access these variables from outside the class.
In the middle of this car class, I have publicly declared two methods called start and stop.

I have also declared another toString method. This is a default overridden method for each class.

// default override method@Overridepublic String toString() {
    return "Car{" +
            "carName='" + carName + '\'' +
            ", numOfWheel=" + numOfWheel +
            ", color='" + color + '\'' +
            '}';
}

Our classes are over. Now the job is to create the object of this car class. For this I will create another class separately. Where we will create all the objects in the car class.
OOPExample.java

package com.oop;

public class OOPExample {

    public static void main(String args[]){


        Car bmw = new Car();
        bmw.carName = "BMW_20";
        bmw.numOfWheel = 4;
        bmw.color = "Green";

        bmw.start(); // output:Car is staring...        bmw.toString(); // output:Car{carName='BMW_20', numOfWheel=4, color='Green'}
        bmw.stop();
        System.out.println(bmw); // output:com.oop.Car@52cc8049
    }
}

So when a class called OOPExample is created, we declare the Java main method in the middle of the class. This is where Java file execution starts.

New keywords are used to create a new object.

Car bmw = new Car ();
So I created an object called bmw. Very easy isn’t it? Huh.

If we print bmw in this case
Car {carName = ‘null’, numOfWheel = 0, color = ‘null’}
I can see it like this. Notice that each variable is assigned with null and zero.

So space has been allocated for bmw object in memory.
Now the task is to assign the value to the bmw object. For this we can do as follows:

bmw.carName = “BMW_20”;

bmw.numOfWheel = 4;

bmw.color = “Green”;

Now if we print bmw then we can see the output as below
Car {carName = ‘BMW_20’, numOfWheel = 4, color = ‘Green’}
We can also access Car class methods if we want. For this we can access the object by typing the name with dot.

bmw.start ();

bmw.stop ();

In the rest of the episode, we will discuss the main concepts of Object-Oriented …