Class And Object In JAVA With Examples – Tutorial

You might have already heard many times JAVA is an Object Oriented Programming which simply means coding in JAVA constantly involve classes and objects. In other words coding in JAVA is not possible without object and class. Even the smallest Hello world program requires declaration of class and method work on object. So let’s understand these two concepts which are really very important in JAVA.


Definition of Class and Object:

Class: The concept of class comes into role when we see certain type of objects or things around us and the common idea or a blueprint behind this type of objects is called Class. In other words class is a properties behind each of the objects or things possess.

For example: Consider you have iPhone, Samsung and Sony devices and you want to represent them in JAVA. To do that you first need to find out what can be the blueprint behind these devices. And here blueprint can be a Mobile because they all are a type of Mobile. So Mobile is a class which can represent iPhone, Samsung and Sony devices here.

How To Declare Class in JAVA:

class Mobile{
/**ToDo Code Here*/
}

Important Note: Class is a representation of similar types of objects or it is a implementation of encapsulation.

  • In terms of java, a class is type declaration means when you want to define a specific type of data for special use then it can be easily obtained with the help of class.
  • Basically, class is blueprint for specific type. In the human language across the world, if you count, there are lots of classes e.g. car, bank, bird, student, employee etc. are very simple example to understand.
  • Class is created with the word class when you want to define it.
    Ex: class Mobile{}
  • Class itself consists of various methods and variable.
  • To call upon class objects of other classes there must be main method with static keyword. Because with the static word method will be called even if you do not create its objects.
  • Classes are provided with special access modifiers that are default, public, private and protected.

Object: Object is an instance of class. Understanding the concept of object is lot easier when considering real life examples around us because the concept is actually based on real life objects. So just look around yourself and you will find yourself surrounded with lots of objects which has a certain characteristics and behaviors.

For example: Your mobile, it’s an example of object which has a lots of characteristics like color, RAM, camera etc and behaviors like calling, messaging etc.

How To Create Object:

Suppose Mobile is the class for which we want to create object name abhi. Below is the code:

//Object abhi is declared
Mobile abhi;
//Object is created using new keyword
abhi = new Mobile();

Alternatively you can create object in one line:

Mobile abhi = new Mobile();
  • Object can be defined as state, behavior of class.
  • Objects are the instance variable of class.
  • Objects are stored through references.
  • Objects are created with new keyword in java. Ex: Mobile abhi = new Mobile();
  •  Every object has its own memory.
  • Objects of class of same type can interact with each other means you can pass your message from one object to other.

Important Note: Objects are run time entity. They are always getting a memory at run time.

Important Note: Object are created from a class and methods or actions are performed on object.


Real Life Example Of Class And Objects:

Here in this example we will display the details of Mobile which three person Abhishek, Rahul and Ravi own. To do that in JAVA first we will create a class Mobile, declare fields for mobile specs (i.e brand, color, camera) and initialized constructor.

Then in main method we will three object for Mobile class. Each object will have the specification details of Mobile which he owns. And finally using System.out.println() method we display the details:

//Class Mobile
class Mobile{
    String brand, color;
    int camera;

//Constructor initialized
    public Mobile(String brand, String color, int camera){
        this.brand = brand;
        this.color = color;
        this.camera = camera;
    } 
    public static void main(String args[]){ 

//Object created
        Mobile abhishek = new Mobile("iPhone","Gold",8);
        Mobile rahul = new Mobile("Samsung","White",13);
        Mobile ravi = new Mobile("Nexus","Black",8);

//Smartphone details displayed for each user
        System.out.println("Abhishek own " + abhishek.brand +" "+ abhishek.color + " color smartphone having "+ abhishek.camera+ "MP"+ " camera");
        System.out.println("Rahul own " + rahul.brand +" "+ rahul.color + " color smartphone having "+ rahul.camera+ "MP"+ " camera");
        System.out.println("Ravi own " + ravi.brand +" "+ ravi.color + " color smartphone having "+ ravi.camera+ "MP"+ " camera");

    } 
}

Output:

Abhishek own iPhone Gold color smartphone having 8MP camera
Rahul own Samsung White color smartphone having 13MP camera
Ravi own Nexus Black color smartphone having 8MP camera

Another Program With Explanation To illustrate The Concept Of Class And Object:

Here in this program class ClassInJava is taken to create data type of student who is having Name, Roll number, Fathers’ Name, Contact Number, Address as fields. As from the name it is clear that these are of different primitive data type and all these are taken together in a class.

Here declaration of constructor of the class is used which initialize the object with predefined values which are passed at the creation of object.

public class ClassInJava {
    
    String name;
    int rollno;
    String fathername;
    String contactno;
    String address;
    
    //Constructor

    ClassInJava(String name,int rollno,String fathername,String contactno,String address){

        this.name=name;
        this.rollno=rollno;
        this.fathername=fathername;
        this.contactno=contactno;
        this.address=address;

    }
}

And next class is ObjectOfClass which is used to create the object of first class. So the main method with the static written in ObjectOfClass. And this is also good prevention to declare first letter of each word of class name as capital.

public class ObjectOfClass {

    /**Simple illustration of how to create an object of given class and how it works
     */

    public static void main(String[] args) {
        
        //Object of class ClassInJava
        ClassInJava object=new ClassInJava("Mr. Abhishek",123,"Mr. Sulekh", "+1-8745733445","#321, South Street, No-3, Ontario");

        System.out.println("Student Name is: " + object.name);
        System.out.println("Roll Number is: " + object.rollno);
        System.out.println("Fathers' Name is: "+ object.fathername);
        System.out.println("Contact Number is: "+ object.contactno);
        System.out.print("Student Address is: "+ object.address);

    }
}

Output:

Student Name is: Mr. Abhishek
Roll Number is: 123
Fathers' Name is: Mr. Sulekh
Contact Number is: +1-8745733445
Student Address is: #321, South Street, No-3, Ontario

Important Points about class and object:

  • The word “Class” came from simula language.
  • Class is blueprint for an entity.
  • In class there are variables and methods.
  • The access modifiers like public, private and protected used in different situation.
  • Objects represent the state and behavior of class.
  • Object is just a memory area or a buffer in heap area in which all the instance data members are getting a memory.
  • In Java “new” operator is only used for allocating a memory for an object only.
  • Memory associated with object is automatically collected by garbage collector.
  • Class with main method having static keyword is mandatory to call upon the object of other classes.
  • All the objects are sharing a same memory location for each static data members.

History about word “Class”: Aristotle was initially consider deeply the concept of type. Simula is language used for simulation of “bank teller problem”. There were lots of object like customer account, bank detail, employee detail and many more. So all these objects were put together in “classes of objects”. And hence the word class was invented for programming languages. That was main origin of word class.

Ask Your Question Or Leave A Reply

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