Classes and objects in java

Let’s learn classes and objects in java.

Classes and objects in java

Class is a collection of objects. It is a logical entity and template or a blueprint from which objects are created.

Syntax:

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

In the below code the public keyword is an access modifier that we use 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


Object definition

Object is an instance of a class. Object is real world entity. Objects consists of identity, state or attribute and behaviour. Now let’s learn how to create objects in java using new keyword.

There are 3 steps to create object in java:

  1. Declaration
  2. Instantiation
  3. Initialization

In declaration we declare a variable name with an object type. For example: Animal dog; where “Animal” is class name and “dog” is reference variable name.

In the second step Instantiation we use “new” keyword. “new” keyword is the main keyword to create object and also memory allocated for an object. For example: dog = new

In initialization along with “new” keyword constructor is called. For example: dog = new Animal(); Here in this step values are put into memory that was allocated.

Here’s the complete syntax : Animal dog = new Animal();

To call the object behaviour or to call method dot operator (.) is used. For example: dog.run(). Here run() is a method.

Let’s see an example class and object in java.

class Bird
{
   public void eat()
   {
      System.out.println("i'm eating");
   }
   public void fly()
   {
      System.out.println("i'm flying");
   }
   public static void main(String[] args)
   {
      Bird sparrow = new Bird();
      sparrow.eat();
      sparrow.fly();
   }
}

Output:

i’m eating
i’m flying


How to initialize objects in java

There are 3 ways – by using reference variable, by method and by constructor.

By using reference variable:

class Vehicle
{
    String color;
    int year;
    public static void main(String[] args)
    {
        Vehicle audi = new Vehicle();
        audi.color = "Black";
        audi.year = 2022;
        System.out.println(audi.color + " " + audi.year);
    }
}

By using method:

class Vehicle
{
    String color;
    int year;
    void initialize(String c, int y)
    {
        color = c;
        year = y;
    }
    void display()
    {
        System.out.println(audi.color + " " + audi.year);
    }
    public static void main(String[] args)
    {
        Vehicle audi = new Vehicle();
        audi.initialize("Blue", 2022);
        audi.display();
    }
}

Click here to learn to initialize objects using constructor.


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.