Classes and objects

Java is an object-oriented programming language which means it is based on classes and objects.

Classes are just blueprints or templates for objects.

An object in Java is just as in real life it has a has part and a does part.

Has Part Of Object

In has part the properties of the object are stored. It is also known as what does an object knows like a car has colour, model, fuelCapacity etc.

Does Part Of Object

This part includes what an object does or the methods of an object like a car does start, accelerate, doorsOpen and stop.

All the properties and methods are derived from classes and properties are set after making the object except in the case of public class and main method.

Example:

class Laptop {

 int ram,rom,ssd,hdd;
 int releaseYear;
 String processor; 

 public void restart {

   System.out.println("restarting");
 }
}

In this above-given example, there is a class called laptop. It has properties like RAM, Processor etc and a method which is "restart".

public class Main {

  public static void main(String args[]) {

    Laptop lapExample = new Laptop();

    lapExample.Ram = 8;
    lapExample.restart();
  }
}

Now, In the main function, an object "lapExample" of class "Laptop" is made, its properties are set and the method "restart" is called.

In this object creation process new Laptop() will be initialized first and a block of space for object will be created in the JRE(Heap) and its address is stored in the reference variable "lapExample". Its initial value is null and then when the properties are set its value becomes the address of the object.