Entity instance là gì

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.

In this difference tutorial, you will learn:

  • What is Class?
  • What is Object?
  • Understand the concept of Java Classes and Objects with an example.
  • Classes and Objects in Java
  • Class Vs. Object
  • Types of Class
  • Uses of Class
  • Use of Object

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.

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

Lets 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, lets 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

Class Vs. Object

Here is the important difference between class and object:

ClassObject
A class is a template for creating objects in program.The object is an instance of a class.
A class is a logical entityObject is a physical entity
A class does not allocate memory space when it is created.Object allocates memory space whenever they are created.
You can declare class only once.You can create more than one object using a class.
Example: Car.Example: Jaguar, BMW, Tesla, etc.
Class generates objectsObjects provide life to the class.
Classes cant be manipulated as they are not available in memory.They can be manipulated.
It doesnt have any values which are associated with the fields.Each and every object has its own values, which are associated with the fields.
You can create class using class keyword.You can create object using new keyword in Java

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.

KEY DIFFERENCES:

  • A class is a template for creating objects in program whereas the object is an instance of a class.
  • A class is a logical entity while object is a physical entity.
  • A class does not allocate memory space on the other hand object allocates memory space.
  • You can declare class only once but you can create more than one object using a class.
  • Classes cant be manipulated while objects can be manipulated.
  • Classes doesnt have any values, whereas objects have its own values.
  • You can create class using class keyword while hand you can create object using new keyword in Java.

You Might Like:

  • What is RAD Model? Phases, Advantages and Disadvantages
  • AR Vs VR: Difference between Augmented reality & Virtual reality
  • 15 Best FREE Download Manager for Windows 10 PC [2022]
  • 10 Best FREE Windows Repair Tools | PC Optimizer Software
  • How to Code a Website from Scratch! 5 Simple Steps

Video liên quan

Chủ Đề