Classes and Objects in Java

What is a Class in Java?

A class can be defined as a template/blueprint from which individual objects are created.

Before you create objects in Java, you need to define a class. A class is a blueprint for the object.

We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

Since many houses can be made from the same description, we can create many objects from a class.

What is an Object in Java ?

Objects have states and behaviors. Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a Class. Objects are often used to model real-world objects you find in everyday life.

An object in Java consists of :

  • State : It is represented by attributes of an object. It also reflects the properties of an Object. These are basically the data members.
  • Behavior :  It reflects the common behaviors of the Object. It is represented by methods of an object.
Classes and Objects in Java
package com.java.core;

public class Human {
    String sex;
    int age;
    int height;
    int weight;

    void eat() {}

    void sleep() {}

    void work() {}

}

Leave a Reply

Your email address will not be published. Required fields are marked *