Classes Overview

  • Eclipse: Oxygen
  • Java: 1.8

Classes and Objects are the basic building block of Object Oriented Programming language. The class can be created by using “class” keyword followed by a reference name.


Classes are the blueprint of object creation. Classes are the user-defined implementation of real-life scenarios. A class defines the state and behaviour of an object.

State – It is the property or variables declared inside a class.

Behaviour – It is the functions or methods created inside a class.

In the following example of a Car class, variables declared inside the class are the properties, and methods which define the start and acceleration of the class Car are the behaviour.

Reuse the Class Properties

Class properties are used to create a new class as a type of an existing class which includes some common properties. And these common properties can be driven to the new class from the existing class.

In the following example, the class Car has some properties like doors, engine, speed, and driver. We will reuse the Car class properties in another “Hello” class.

To access the object of the “Car” class we have to create the object of the “Car” class in “Hello” class.

new Car(); is the object of Car class which is referenced by obj variable.



Classes-Getters and Setters:

In Java, Getter and setter methods are used to retrieve and update the private variables. When you hide the implementation of the object of the class from the outer world, you declare them as private. As private members of the class are not accessible from outside the class, so we use the getter and setter method to retrieve and update the values of private members.

In the following example, we have declared some private variables inside the class. We will write getter and setter methods for the variables.

We can manually write the getter and setter method for variable “speed”. The setter method is a parameterized method with return type as void, which is used to update the value. Getter method has a return type which will be the same as the data type of private variable.

To access the properties (variable) and functionalities (methods) of “car” in another class, we have to create the object of car class.



You can generate the getter and setter method for all remaining variable in the class, from the source menu in eclipse.

Source >> Generate Getter and Setter

After generating the getter and setter method you can easily set the values.

Adding functions to a class

Likewise, the class has properties, it also has functions or methods. In the following example, we will create a function in the class.

In this code we are validating the car’s behavior on multiple conditions. “run” function will return the behavior according to the applied conditions. Properties of the car are set in different class.

Again, we call the properties and function of Car class by using the object of Car class.


car is running

Contributed by: Poonam Tomar


Buy me a coffee!



  • Get video tutorials
  • Project files set
  • Get Certified

Leave a Reply

Notify of
Close Menu