Classes and objects in java

Let’s learn what is classes and objects in java?

Classes and objects in java

Class is a template or a blueprint for creating objects. Classes are fundamental component of object-oriented programming in java and other languages for that matter.

Syntax:

access-modifier class ClassName
{
   // variables
   // constructors
   // methods
}

In the below code the public keyword is an access modifier that we use here to determine what access we want to allow others to have to this new class. Here the word public means unrestricted access to the class.

public class Employee
{
   
}

Basically a class consists of intrinsic elements, namely

  • Access modifiers
  • A class name
  • extends keyword (superclass and subclass)
  • Methods
  • Variables
  • Interface
  • { } left and right curly brace. It’s actually code block defined or used to define start and end of a class. We can put our code in between those in the form of statements.

Another important element in a class is constructor. Constructor is used to initialize objects.


Object definition

Object is an instance of a class. Object is a real world entity. Objects are a fundamental part of object-oriented programming.

Laptop obj;  // we have defined an object "obj" of type Laptop

Software object stores its state in fields, and we know fields as variables. Fields disclose their behaviour with methods. For example in a laptop, state might be the amount of RAM it has, the operating system it’s running, the hard drive size, and the size of the screen.

Looking at behavior for a laptop it might be booting up, shutting down, outputting some sound, printing something, drawing something on screen and so on. Let’s see an example.

class Laptop
{
   // state or field
   public String ram = "16GB";
   public String operatingSystem = "Windows";  

   // behavior or method
   public void booting()
   {
      System.out.println("Laptop is booting.");
   }
}

class TestClass
{  
   public static void main(String args[])
   {
      Laptop obj = new Laptop();  
      System.out.println(obj.ram);  
      System.out.println(obj.operatingSystem);
      obj.booting(); 
   }
}

Output:

16GB
Windows
Laptop is booting.

In the above java program fields and methods represent state and behavior of an object. Fields (variables) are used to store data and methods are used to execute some operation.

In the above program “Laptop” is a template or blueprint. Using this template we can create any number of laptops. All the laptops will share fields and methods of the blueprint.


Creating multiple objects

Meanwhile we can create multiple objects of one class. In the below code we have used “new” keyword with constructor of class to create object. ultrabook, ultraportable and macbook are names of objects.

Object names are used to access fields and methods of the class. Dot (.) is used along with object name to access members of a class.

class Laptop
{
   // state or field
   public String ram = "16GB";
   public String operatingSystem = "Windows";  

   // behavior or method
   public void booting()
   {
      System.out.println("Laptop is booting.");
   }
}

public class TestClass
{
   public static void main(String[] args)
   {
      Laptop ultrabook = new Laptop();
      System.out.println("First object: ");
      System.out.println(ultrabook.ram);  
      System.out.println(ultrabook.operatingSystem);
      ultrabook.booting();
      System.out.println("\n");
      Laptop ultraportable = new Laptop();
      System.out.println("Second object: ");
      System.out.println(ultraportable.ram);  
      System.out.println(ultraportable.operatingSystem);
      ultraportable.booting();
      System.out.println("\n");
      Laptop macbook = new Laptop();
      System.out.println("Third object: ");
      System.out.println(macbook.ram);  
      System.out.println(macbook.operatingSystem);
      macbook.booting();
   }
}

Output:

First object:
16GB
Windows
Laptop is booting.

Second object:
16GB
Windows
Laptop is booting.

Third object:
16GB
Windows
Laptop is booting.