Classes and Objects in Java

Beginners code Computer Skills Java Mobile Development Web Development

From the very beginning of this tutorial, we have learned that a Java program must have a class. All the methods and data variables of the program are enclosed within that class. So in this article, we will learn the characteristics and behavior of classes and objects in Java in details.

For the video tutorial, you can refer to this:

An analogy of a Class:

Consider class to be a set, let us say birds.

Now, there are many birds with different names and characteristics but all of them will share some common properties.

Like, all of them will have feathers, wings, beak, and skeleton.

So here, Birds is the Class and feathers, wings, beak, the skeleton is their properties.

Again, the birds can perform activities like flying, laying eggs. So the Birds class can have functions for these activities.

Now let’s see the features of class in technical terms!

Class:

  • A Class is a user-defined data-type
  • It holds both data members as well as member functions.
    • Data members are the data variables
    • Member functions are the functions that can manipulate Data members.
    • Together, the data members and the member functions define the properties and behavior of the objects in that Class.
  • Class is a skeleton of the objects. When you define a Class, you actually define the blueprint of the data type.
  • Class is a logical container. When we create a class, it does not allocate any memory.
  • Creating a class defines what an object of the class will consist of and what operations can be done on its objects.
  • In Java, a class can contain 4 types of members.
    • Data members (variables)
    • Method members (functions)
    • Block members
    • Constructor members

Here is a sample code for Birds Class.

public class Birds {
  
// Data members
  String name;
  String feathers;
  String wings;
  String beak;
  String skeleton;

// Method members
  public void fly(String name) {
    
    System.out.println(name + " can fly !!");
  }
  
  public void lay_eggs(String name) {
    
    System.out.println("Birds lay eggs !!");
  }
}

 

The analogy of Objects:

For the Class of Birds, Parrots, Sparrows, Crows will be called as the objects of the Birds class. This is because the Parrots, Sparrows, and Crows will inherit the properties of the Birds class and will inherit its member functions (functions like flying and laying eggs in simple word) as well.

Objects:

  • Objects are implementations of a class. It will have the same properties and behavior as that of the Class.
  • The object will have access to all the methods and data members of its class.
  • When an object of a class is created, the memory allocation takes place.
  • Defining Objects in Java:
    An object is created in the same way as a variable of any predefined data type is defined.Data_type variableName;ClassName ObjectName;
  • Thus, an object is a variable of the Data type as the Class type. Just as an integer variable possesses all the characteristics of Integer Data type similarly the object will exhibit all the characteristics of the Class.
  • Multiple objects can be created for the same Class.
  • Each object has its own individual copy of the data members and works upon them.
  • The methods of a Class are called with the help of objects in order to perform the assigned tasks.

Here is an example of creating objects for Birds class.

public class Birds {
  
  String name;
  String feathers;
  String wings;
  String beak;
  String skeleton;

  public void fly(String name) {
    
    System.out.println(name + " can fly !!");
  }
  

  public void lay_eggs(String name) {
    
    System.out.println("Birds lay eggs !!");
  }
  
  public static void main(String[] args) {
    
    Birds parrot = new Birds();
    parrot.name = "PARROT";
    parrot.beak ="red";
    parrot.feathers="green";
    System.out.println("Name is "+parrot.name);
    System.out.println("Beak is "+parrot.beak);
    System.out.println("Feathers are "+parrot.feathers);
    parrot.fly(parrot.name);
    parrot.lay_eggs(parrot.name);
    
    
    Birds sparrow = new Birds();
    sparrow.name = "SPARROW";
    sparrow.beak="black";
    sparrow.feathers="brown";
    System.out.println("Name is "+sparrow.name);
    System.out.println("Beak is "+sparrow.beak);
    System.out.println("Feathers are "+sparrow.feathers);
    sparrow.fly(sparrow.name);
    sparrow.lay_eggs(sparrow.name);
  }
  
}

 

Output:

We can see that each object retains its own copy of the data members and member functions.

Birds class output

 

Objects vs Class:

The below table sums up the major differences between Classes and Objects.

ClassesObjects
Outlines description of methods and data that object can containImplementation of properties defined in a class
Creation of class doesn't allocate any memoryCreation of an object of a class allocates memory
It is a logical entityIt is a physical entity
Declared only onceMultiple objects for one class can be defined