Review
Best Keyboard for Programming & Coding in 2021
Programmers spend most of their days on a computer designing, writing, and testing code. This...
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:
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.
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,
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.
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
Here is the important difference between class and object:
Class | Object |
A class is a template for creating objects in program. | The object is an instance of a class. |
A class is a logical entity | Object 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 objects | Objects provide life to the class. |
Classes can't be manipulated as they are not available in memory. | They can be manipulated. |
It doesn't 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 |
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.
Here are the important uses of class:
Here are the important uses of an object
Programmers spend most of their days on a computer designing, writing, and testing code. This...
$20.20 $9.99 for today 4.6 (115 ratings) Key Highlights of Java Programming Language PDF 265+...
What is ServiceNow? ServiceNow is a cloud-based software platform for IT Service Management (ITSM) which...
$20.20 $9.99 for today 4.5 (114 ratings) Key Highlights of C# Tutorial PDF 243+ pages eBook...
What is XML? XML is a markup language which is designed to store data. It's popularly used or...
{loadposition top-ads-automation-testing-tools} What is DevOps Tool? DevOps Tools help automate the...