Class and Object Concept in Java

Rumman Ansari   Software Engineer   2022-03-02   19033 Share
☰ Table of Contents

Table of Content:


Class and Object

we have already discussed little bit about class and object in this section Basic concept of class and object if you don't know about this please read it first then go further.

Classes and objects are the fundamental components of OOP's. Often there is a confusion between classes and objects. In this tutorial, we try to tell you the difference between class and object.

First, let's understand what they are,

What is Class in Java

A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.

class and object in java

A class in Java can contain:

  • fields
  • methods
  • constructors
  • blocks
  • nested class and interface
class and object in java

Syntax to declare a class:

class Class_name{  
    field or variable ;  
    method like main() and others ;  
}  

What is an Object

An object is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. Object determines the behavior of the class. When you send a message to an object, you are asking the object to invoke or execute one of its methods.

From a programming point of view, an object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed as class hierarchies.

What is the difference between Object & class?

class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind.

An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.

concept of class, Object and methods

You can see the picture of three different breeds of dogs below. Here Dog is a class and three different type of breeds are called as three different objects like : Dog1Object
Dod2Object
Dog3Object

class and object in java

List down the differences between them. Some of the differences you might have listed out may be breed, size, color, age etc. If you think for a minute, these differences are also some common characteristics shared by these dogs. These characteristics (breed, size, color, age) can form a fields or data members for your object.

class and object in java

Next, list out the common behaviors of these dogs like eat, run sleep, name etc. So these will be the actions for our objects. These behavoiurs(eat, run sleep, name) is called as methods.

class and object in java

So far we have defined following things:

  • Class - Dogs
  • Data members - breed, size, color,  age etc.
  • Methods- eat(), run(), sleep(), name().
class and object in java class and object in java

Sample Program for the above concept


public class Dog {
	
	// instance variable
	String breed;
	String size;
	String color;
	int age;

	public static void main(String[] args) {
		// create object here
		Dog Dog1Object = new Dog();
		Dog Dog2Object = new Dog();
		Dog Dog3Object = new Dog();
		
		// Data input for Dog Object 1
		Dog1Object.breed = "Beagle";
		Dog1Object.size  = "Large";
		Dog1Object.color = "Light Gray";
		Dog1Object.age   = 5;
		
		// Data input for Dog Object 2
		Dog2Object.breed = "Buldog";
		Dog2Object.size  = "Large";
		Dog2Object.color = "Orange";
		Dog2Object.age   = 6;
		
		// Data input for Dog Object 3
		Dog3Object.breed = "German Shepherd";
		Dog3Object.size  = "large";
		Dog3Object.color = "white and Orange";
		Dog3Object.age   = 6;
	
		// print all data from objects
		System.out.println("Dog Object 1: \n Breed: "+Dog1Object.breed+"\n Size: "+Dog1Object.size+"\n Color: "+Dog1Object.color+"\n Age: "+Dog1Object.age);
		System.out.println("Dog Object 2: \n Breed: "+Dog2Object.breed+"\n Size: "+Dog2Object.size+"\n Color: "+Dog2Object.color+"\n Age: "+Dog2Object.age);
		System.out.println("Dog Object 3: \n Breed: "+Dog3Object.breed+"\n Size: "+Dog3Object.size+"\n Color: "+Dog3Object.color+"\n Age: "+Dog3Object.age);

		
	}

}

Output

Dog Object 1: 
 Breed: Beagle
 Size: Large
 Color: Light Gray
 Age: 5
Dog Object 2: 
 Breed: Buldog
 Size: Large
 Color: Orange
 Age: 6
Dog Object 3: 
 Breed: German Shepherd
 Size: large
 Color: white and Orange
 Age: 6

Practical Approach of Class and Object

main() Method within class

In this example, we have created a Student class that have two data members name and rollNo. We are creating the object of the Student class by new keyword and printing the objects value.
Here, we are creating main() method inside the class.

Save this Program as:Student.java
class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable

 public static void main(String args[]){
  Student s1=new Student();//creating an object of Student
  System.out.println(s1.name); //accessing member through reference variable
  System.out.println(s1.rollNo);//accessing member through reference variable
 }
}

Output:

null
0
Press any key to continue . . .

It gives null and 0 output because we know that default value number is 0 and for object references it is null.

main() Method outside class

We create classes and use it from another class using instance of that class(object). It is a better approach than previous one. Let's see a simple example, where we are having main() method in another class. We can have multiple classes in different java files or single java file. If you define multiple classes in a single java source file, it is a good idea to save the file name with the class name which has main() method.

Save this Program as:MainClass.java
// this is a different class
class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable
}

//this is main class
class MainClass{
 public static void main(String args[]){
  Student s1=new Student();//creating an object of Student
  System.out.println(s1.name); //accessing member through reference variable
  System.out.println(s1.rollNo);//accessing member through reference variable
 }
}

Output:

null
0
Press any key to continue . . .

It gives null and 0 output because we know that default value number is 0 and for object references it is null.

Initialize object

There is three Ways to initialize object

  1. By reference variable
  2. By method
  3. By constructor

Initialization through reference

class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable
}

class MainClass{
 public static void main(String args[]){
  Student s1=new Student();//creating an object of Student
   s1.name = "Ramboo";   //Initialization through reference
   s1.rollNo = 21;     // Initialization through reference
   System.out.println(s1.name+"'s Roll No: "+s1.rollNo);
 }
}

Output:

Ramboo's Roll No: 21
Press any key to continue . . .

Initialization through method


class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable

 void methodforInti(String s, int r)
 {
	name = s;
	rollNo = r;
 }

 public static void main(String args[]){
  Student obj1=new Student();
    obj1.methodforInti("Ramboo",21);
    System.out.println(obj1.name+"'s Roll No: "+obj1.rollNo);
 }
} 

Output:

Ramboo's Roll No: 21
Press any key to continue . . .

Initialization and dispaly through method:

 
class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable

   void methodforInti(String s, int r)
   {
	name = s;
	rollNo = r;
   }

   void methodforDisplay()
   {
 	System.out.println(name+"'s Roll No: "+rollNo);
   }

 public static void main(String args[]){
  Student obj1=new Student();
    obj1.methodforInti("Ramboo",21);
    obj1.methodforDisplay();
 }
}

Output:

Ramboo's Roll No: 21
Press any key to continue . . .

Initialization through constructor

 

class Student{
 String name; //field or data member or instance variable
 int rollNo;//field or data member or instance variable

 Student(String s, int r) // this is a constructor
 {
	name = s;
	rollNo = r;
 }

  void methodforDisplay()
  {
 	System.out.println(name+"'s Roll No: "+rollNo);
 }

 public static void main(String args[]){
  Student obj1=new Student("Ramboo",21);
    obj1.methodforDisplay();
 }
}

Output:

Ramboo's Roll No: 21
Press any key to continue . . .