Class vs Object

What is Class?

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. It provides initial values for member variables and member functions or methods.

What is Object?

An object is nothing but a self-contained component that consists of methods and properties to make a data useful. It helps you to determines the behavior of the class. For example, 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 that has a memory location allocated. The object is designed as class hierarchies.

Class vs Object – Difference Between Them

Here is the important difference between class and object:

Understand the concept of Java Classes and Objects with an example.

Let’s take an example of developing a pet management system, specially meant for dogs. You will need various information about the dogs like different breeds of the dogs, the age, size, etc. You need to model real-life beings, i.e., dogs into software entities.

Moreover, the million dollar question is, how you design such software? Here is the solution- First, let’s do an exercise. You can see the picture of three different breeds of dogs below.

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

Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. So these will be the actions of our software objects.

So far we have defined following things,

Class: Dogs Data members or objects: size, age, color, breed, etc. Methods: eat, sleep, sit and run.

Now, for different values of data members (breed size, age, and color) in Java class, you will get different dog objects.

You can design any program using this OOPs approach.

Classes and Objects in Java

In the below program, we have declared a class called Dog. We have defined an object of the class called “maltese” using a new keyword. In the last statement System.out.println(maltese.getInfo()); we are displaying dog information like Breed, Size, Age, Color, etc.

// Class Declaration class Dog { // Instance Variables String breed; String size; int age; String color;

// method 1
public String getInfo() {
    return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color);
}

} public class Execute{ public static void main(String[] args) { Dog maltese = new Dog(); maltese.breed=“Maltese”; maltese.size=“Small”; maltese.age=2; maltese.color=“white”; System.out.println(maltese.getInfo()); } }

Output: Breed is: Maltese Size is: Small Age is:2 color is: white

Types of Class

Following are the important types of class: Derived Classes and Inheritance A derived class is a class which is created or derived from other remining class. It is used for increasing the functionality of base class. This type of class derives and inherits properties from existing class. It can also add or share/extends its own properties. Superclasses: A superclass is a class from which you can derive many sub classes. Subclasses: A subclass is a class that derives from superclass. Mixed classes A mixed class is one more functionality that helps you to inherit the properties of one class to another. It uses a subset of the functionality of class, whereas a derive class uses the complete set of superclass functionality.

Uses of Class

Here are the important uses of class:

Class is used to hold both data variables and member functions. It enables you to create user define objects. Class provides a way to organize information about data. You can use class to inherit the property of other class. Classes can be used to take advantage of constructor or destructor. It can be used for a large amount of data and complex applications.

Use of Object

Here are the important uses of an object

It helps you to know the type of message accepted and the type of returned responses. You can use an object to access a piece of memory using an object reference variable. It is used to manipulate data. Objects represent a real-world problem for which you are finding a solution. It enables data members and member functions to perform the desired task.