
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Object and Class in Java
Java is an object-oriented programming language, which means that everything in Java is represented as a class and object. Every entity with state and behavior is an object. And, classes are the templates to create objects. It contains data and functions so that it can later be accessed by their objects wherever and whenever they are needed. The main task of objects is to get the data present in the classes or use the functions in those classes to manipulate the data.
It is compulsory for a class to have an object. Otherwise, it can't be used. But, an object can only be created for an existing class. Read this article to learn more about objects and classes in Java and how they are different from each other.
What are Classes in Java?
A class is a user-defined data type that acts as a blueprint for designing the objects in it. It is said to be a container that stores objects of similar type.
Points to Remember about Java Classes
Every class consists of data members and member functions which can be accessed by their objects. Here, data members are variables and member functions are methods to manipulate the variables.
A class is created using the keyword "class". A single class can have more than one object. Once a class is declared, it can have as many as objects it requires.
Defining a class doesn't allocate any memory for it. Memory is created whenever it is instantiated. A class is declared at the beginning of a program that can be accessed anywhere throughout the program using their objects.
A class encapsulates the data members and member functions in it. If fruit is considered as a class, then different kinds of fruits such as apple, mango, grapes, etc., are considered as objects.
Features of Java Classes
Following are the benefits of using Classes:
Classes promote the concept of data encapsulation whose main goal is data hiding. It prevents unwanted modification and accessing of data by others. A class cannot be accessed unless it is called by its object
Large code can be broken into classes that helps to reuse the code.
It allows inheritance. One class can inherit the functions of other classes. Class that inherit properties is known as a sub class or derived class and the other one from which properties are inherited is known as a super class or base class
What are Objects in Java?
An instance of class is known as an object. It is a real- world entity that is used to access the data members and member functions of a class. An object is said to be a variable for class.
Every time an object is created, memory is allocated for it. The methods of a class can be accessed every time when an object is invoked.
Each and every object has an associated state and behavior. If we consider cat as a class, it consists of information of different breeds of cats, which are its different objects. Each object has some common attributes such as breed, color, age, etc. These elements form the data members of that class and the functions those objects perform are eating, sleeping, walking, etc., which are known as their member functions. So, as these data members are unique to each object, we get different objects for the class "cat".
Based on their work, objects are classified into the following types:
Function objects
Container objects
Immutable objects
Factory objects
Features of Java Objects
Here are some of the benefits of using Objects:
Objects are used to access the class members
We can invoke and use the classes anywhere in the code
They are used to change the data
Objects allow easy debugging
Example of Objects and Classes in Java
In the following example, Puppy is a class and myPuppy is an object:
public class Puppy { public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is: " + name); } public static void main(String []args) { // Following statement would create an object myPuppy Puppy myPuppy = new Puppy("tommy"); } }
Output
If we compile and run the above program, then it will produce the following output:
Passed Name is: tommy
Differences: Objects vs Classes
The following table highlights the major differences between Objects and Classes:
Class |
Object |
---|---|
A class is said to be a blue print for the objects |
An instance of class is known of an object |
Class is a user defined data type |
Object is a variable of a class |
It contains data members and member functions |
Data and methods of a class can be accessed by these objects |
A class can have a number of objects |
An object is bound to only a single class |
No memory is allocated for a class |
Memory is allocated every time when the object is created |
It is a logical entity |
It is a physical entity |
A class can be created only once |
Objects can be created number of times |
They can't be manipulated as it is not associated with any memory |
Objects can be manipulated |
It doesn't have values bound to it |
Objects have their own values |
A class is created at the beginning of a program |
Objects can be created anywhere throughout the program |
Conclusion
Classes and objects together protect data and helps in achieving data abstraction, encapsulation, polymorphism, inheritance etc., hence, these classes and objects form the base of object-oriented programming.