In this tutorial, we are going to learn how to understand the concepts of classes and objects in Java.
Java is a widely used object-oriented programming language that uses classes and objects to solve real-time problems.
What is a Class?
In Java, a class is a blueprint for creating an object. The Class represents a group of objects having similar properties/attributes and behavior/characteristics.
The class has some properties as mentioned below:
- It is not a real-world entity.
- It does not occupy memory.
- It can contain a method, constructor, data member, and nested class.
Class can declared as follows:
class <name of the class>{
//It can contain method, constructor, data member, nested class
}
For example, the bird-type Parrot is a class while a particular parrot named Alpha is an object of the Parrot class.
//creating a class class Parrot{ // a method, constructor, or a data member }
Examples of Class
Example 1
Let’s see this example:
//Java program for understanding class class student{ //data members or attributes String name; int year; int id; public static void main(String args[]){ // creating an object of student student s=new student(); System.out.println("Name : " + s.name); System.out.println("Year : " + s.year); System.out.println("Id : " + s.id); } }
Output
Name : null Year : 0 Id : 0
Example 2
Let’s see this example:
class student{ String name="abc"; int year=3; int id=50; public static void main(String args[]){ student s=new student(); System.out.println("Name : " + s.name); System.out.println("Year : " + s.year); System.out.println("Id : " + s.id); } }
Output
Name : abc Year : 3 Id : 50
What is a Object?
In Java object is a basic unit of Object-oriented programming and it represents the real-life entities. Object is an instance of a class. An object can access the attributes and methods defined in the class.
When a class is defined, no memory will be created and allocated, until an object of the class is created. A single class can contain multiple instances.
For example, suppose pet is a class then dog, cat, parrot, etc can be considered as objects of a class pet.
Syntax for creating an object in java:
classname Object = new classname();
//for Pet class Pet dog = new Pet(); Pet cat = new cat();
Here we have used new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class.
In the above example we use Pet() as the constructor along with new keyword and same as the class name. And we use dog and cat as the names of the objects.
Examples of Object
Example 1
Let’s see this example:
class ObjectExample{ int a = 4; public static void main(String[] args){ ObjectExample obj = new ObjectExample(); System.out.println("Value of a : " + obj.a); } }
Output
Value of a : 4
Example 2
Let’s see this example:
//example for creating multiple objects in the class class MulObjects{ int a = 4; public static void main(String[] args){ //creating object of the class MulObjects obj1 = new MulObjects(); MulObjects obj2 = new MulObjects(); System.out.println(obj1.a); System.out.println(obj2.a); } }
Output
4 4
Understanding classes and objects in Java is a fundamental concept . Classes define the structure and behavior of the objects, while objects are the instances of the class. By using the concepts of encapsulation, polymorphism, inheritance, abstraction -one can write more maintainable, modular code.