Class and Object



A class is blueprint for creating objects. It defines the structure and behavior. It can contain methods and variables while object consists of a state and related behavior that is created from a class.

Difference between Object and Class

1. Object is an instance of a class while Class is a blueprint  from which objects are created.

2. Object is a real world entity such as table, chair, phone ,etc while Class is a group of similar objects.

3) Object is a physical entity while Class is a logical entity.

4) Object is created through new while Class is created  using class keyword.

5) Object is created many times as per requirement while Class is declared once.

6) Object allocates memory when it is created while Class doesn't allocated memory when it is created

Programming Example of Class and Object

//defining or creating  Payroll class

class Payroll { 

  int Empno;  //data member or instance variables

  String Empname; //data member or instance variables

  String empDept;//data member or instance variables

  String empPost;//data member or instance variables

  float empsalary;  //data member or instance variables

//Creating a method called dataentry

  void dataentry(int no, String name,String dept, String post, float sa) { 

     Empno=no; 

      Empname=name;

      empDept=dept;

      empPost=post;

      empsalary=sa;

  } 

//Creating a method called emprecord

  void emprecord(){System.out.println(Empno+" "+Empname+" "+empDept+ " "+ empPost+ " "+empsalary);} 

} 

//Creating another class Personnel with main method inside

public class Zap { 

public static void main(String[] args) { 

//Creating an object of Payroll

 Payroll record1=new Payroll(); 

  Payroll record2=new Payroll(); 

  Payroll record3=new Payroll(); 

  Payroll record4=new Payroll();

  System.out.println("==========================================");

  System.out.println("Analysis of Payment ");

  System.out.println("==========================================");

  //accessing member through reference variables or actual parameters

  record1.dataentry(11,"Joe James  ", "Sales      ","Manager     ", 18000); 

  record2.dataentry(12,"kyari Bello  ","Production  ","Accountant  ",8000); 

  record3.dataentry(13,"Ahmed Bob   ","Sales        ","Clerk    ",59000);

  record4.dataentry(14,"Johnson AJAYI   ","Sales      ","Clerk    ",8999);

  record1.emprecord();  //Calling of the method

  record2.emprecord(); 

  record3.emprecord(); 

  record4.emprecord();

} 

}

 Output

==========================================

Analysis of Payment

==========================================

11 Joe James   Sales       Manager      18000.0

12 kyari Bello   Production   Accountant   8000.0

13 Ahmed Bob    Sales         Clerk     59000.0

14 Johnson AJAYI    Sales         Clerk     8999.0

Explanation

From the above example Two Classes are defined. We have the Payroll class and the Zap class.

In the Payroll class instance variables and two methods are created. The instance variables in the Payroll class are :

  Empname;

  empDept;

  empPost;

  empsalary;

while the two methods are void emprecord(){      } and  void dataentry(list of parameters){}

The class from above illustration contains methods and variables. The Keywords class is used to create both the Payroll class and Zap class which is the main class.

The Payroll class uses in creating the following objects in the main class . e.g

//Creating an object of Payroll

 Payroll record1=new Payroll(); 

  Payroll record2=new Payroll(); 

  Payroll record3=new Payroll(); 

  Payroll record4=new Payroll();

We use Payroll in creating multiple objects in the above example and each of these objects has different name. e.g record1, record2, record3 and record4.

These objects are all instance of the Payroll Class. new keyword is used in creating those objects. These objects created are used in calling  the method of the class. i.e record1, record2, record3 and record4.

 For example.

  record1.dataentry(11,"Joe James  ", "Sales      ","Manager     ", 18000); 

  record2.dataentry(12,"kyari Bello  ","Production  ","Accountant  ",8000); 

  record3.dataentry(13,"Ahmed Bob   ","Sales        ","Clerk    ",59000);

  record4.dataentry(14,"Johnson AJAYI   ","Sales      ","Clerk    ",8999);

  record1.emprecord();  //Calling of the method

  record2.emprecord(); 

  record3.emprecord(); 

  record4.emprecord();

Reference 

Eck, D. J. (2019). Introduction to programming using Java, version 8.1. Hobart and William Smith College. http://math.hws.edu/javanotes

https://www.javatpoint.com/object-and-class-in-java

Gallardo, R., Hommel, S., Kannan, S., Gordon, J., and Zakhour, S.B. (2016). The Java tutorials. Oracle.

What is an object? https://docs.oracle.com/javase/tutorial/java/concepts/object.html

Stephen Olubanji Akinpelu

Previous Post Next Post