Classes In Java

Objects and classes are the two basic principles of Object Oriented Programming. They can be considered the two sides of a coin, which cannot be disintegrated. It is insignificant to discuss an object without referring to its class. You have learned that an object contains some characteristics and behaviors. Each object belonging to a class contains an entire set of data and functions included in the class. The behavior (method) is used to manipulate the data (state).

What are Classes in Java?

Class is a group of similar types of objects, which possesses the same attributes and behavior It also defines the abstract characteristics of an object, including its attributes, fields, or properties and the behaviors (the things it can do, or methods, operations or features) You may say that a class is a blueprint or factory that describes the nature of an object.

A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.

For example, if we define a class ‘Car’, then different types of cars like Maruti. Santro, Indica, and Nano are referred to as objects under that category. It means that the different types of cars are similar objects belonging to the class ‘Car In some applications, we can also create a class within the class to represent the system. If we want to create a class People’ then the classes inside the class may be Men, Women, etc.

Let us consider “Pokemon” as a class, where different power viz. Fire, Water, Grass, and Electric type can be referred to as their objects. Each object has some characteristics and behaviors, which are defined for the class “Pokemon”.

Class In Java
Example Of Class And Objects In Java

With reference to the above example, if the class “Pokemon” possesses attack, defense, and speed as its characteristics then the relevant functions could be cal_attack(), cal_defense(), etc. It means that the objects (Fire, Water, Grass, and Electric type) belonging to the class “Pokemon” adopt characteristics and behaviors automatically.

Some Facts about a Classes

How to create an object of classes?

As discussed above that a class provides the blueprint for objects. So, basically, an object is created from a class. In Java, the new keyword is used for allocating the dynamic memory of an object. There are three steps when creating an object from a class:

  • Declaration: It uses class as a datatype along with an object.
  • Instantiation: The ‘new’ keyword is used for allocating an object in the memory.
  • Initialization: It is used for calling a constructor to initialize attributes of an object.

With reference to the above illustration, the process of creating an object of the class type “Pokemon” is written as:


Pokemon Fire = new Pokemon(); 
Pokemon Water = new Pokemon(); 
Pokemon Grass = new Pokemon();

Attributes of a classes

As you know, every object has its own identity whereas a class contains data and methods (functions). The data are referred to as data members and methods as member methods. The belongings of a class ie, data members, and member methods contained within it, are termed attributes of a class.

Thus, an attribute deals with the contents of an object to make the object distinct.

The objects may differ in their attributes:

For example: suppose, you have two television sets at home but they may be physically distinct from each other with different attributes (ie. the number of channels, screen size, power consumption, etc.).

Classes as abstraction

An abstraction is thinking based on general ideas rather than our real world things and events. There can be many classes defined for an object of a class, which represents an abstraction. It is a named collection of attributes and behaviors, which is related to serving the purposes.

Classes are an object factory

The term factory typically is used for holding similar kinds of objects within it. Generally, an object is created by using information available in the class. It also contains details of an object’s internal data and when an object is brought into existence, it is called instantiation.

A good example is to think of a class as a blueprint (Map) of a house, whereas an object is an actual house. You can build many houses (objects) from the same blueprint (Map).

Thus, we can conclude that class is a prototype of an object. Each object belonging to a specific class possesses the data and methods defined within the class. It produces objects of a similar type. Hence, it is termed an object factory.

Classes is a user defined data type

Primitive data types are not sufficient. In the real world, we have much more complicated objects. Object Oriented Programming allows us to model real world objects. User-defined classes combine the data and methods as integrated components.

Thus, we can say that Java language uses some pre-defined data types viz. int. float, char These data types are also known as ‘Composite Data Types’, which provide some preliminary facilities in Java programming. When they are used to declare any variable: then the variable would possess its built-in characteristics. Hence, these types are called built-in data types. Similarly, a user may create a data type and declare certain characteristics and behavior within it. This can be done by using a class. This is the reason why a class is referred to as user defined data type.

Classes are composite data type

Classes are the prototype of an object which contains all fundamental data types viz. byte, short, int, long, float, double, char, boolean.

Any data type that is based on primitive (fundamental) data types is known as a composite data type. Since the class uses these data types to perform various tasks, so the class is also known as a ‘Composite Data type’.

Object is an instance of a classes

In Java language, where each object created from a class, is called an instance of that class. Creating an instance of a class is sometimes referred to as instantiating the class.

A real world example of an object would be “Pikachu”, which is an instance of a class called “Pokemon”. As we already have discussed that the data members of a class are also referred to as instant variables. These instant variables are embedded automatically within an object at the time of its creation. Hence, an object is referred to as an instance of a class.

Differences between Classes and Objects

1. It is a representation of only an abstraction.1. It is a real and unique entity having some characteristics and
2. It is an object producer and hence, called a blueprint of a set of objects.2. It is created with the use of new.
3. It is known as an ‘Object Factory’.3. It is known as an ‘Instance of a Class.
Differences between Class and Object

What is Constructor?

When a class is created in Java, an empty constructor is created by the compiler by default. If you do not create any constructor, then the constructor of the above class is given below.

The constructor is run at least once when the object is created. The constructor is named after the class. You can also define more than one constructor for a class.

What are the Advantages of Classes?

  • Security: Your variables and methods are in classes. Classes provide 3-level access protection. No one can access your class members unless you yourself want.
  • Reduce complexity: By using Classes, you avoid the scattering of variables and methods. Classes make your program simple and its complexity goes away.
  • Separation: You have many types of data in any program. Different operations are performed on different data, which you perform through methods. Using Classes, you can separate one type of data and its related operations (methods) from another type of data and its related operations (methods).
  • Focus on Data: When you use classes, the entire focus of the program is on the data. You create methods for data only and hide data only. Classes are a data focused approach which is very important to create useful software.


Everything in Java is a class. What is class after all? And why everything in java is class? Let’s try to know. First of all, let me tell you that class is a type. As there are data types. You also create variables of class type. The only difference is that class type variables are called objects. Through Class, you create a group structure of many variables and functions and identify them with a name. Many objects can be created on the basis of this structure.

Share your love