Java Object Oriented Programming

Java Class and Object

Class and Object are the basic concept of OOP paradigm which cannot be learned separately. So we will learn them simultaneously.


Class is generally considered as the blueprint/template that describes the state or behavior that object of its type support.

Any number of objects can be created from the class. Thus object is considered as instance of the class.


This is definition which one can find in any book or tutorial. But what does it actually mean?


Why a class can be considered as a blueprint/template?

Why object is instance of the class?

Let’s try to find out taking real life example.


If we look on the road, we can see different type of objects such as cars, buses, trucks running here and there. Each of these can be considered as an instance of a class Vehicle. And these instances are born from the same set of blueprints and thus consist of same components.


Although car, truck, bus, auto rickshaw belongs to class Vehicle but each of them has different state and behavior.



State: Max speed = 220 Km/hr, Model = Swift Dzire Tour, Seats = 5, etc

Behavior: Booking Fare = Rs 50 per person, Self start, etc.



Auto rickshaw

State: Max speed = 60 Km/hr, Model = Swift Auto 1, Seats = 4, etc.

Behavior: Rs 5 per person, manual start, etc


Above concept is explained through code snippet:


Source: Vehicle.java


public class Vehicle {

       int maxSpeed;

       String model;

       int seats;

       int price_per_seat;


       public int fareCollection(int seat, int price_per_seat){

              int totalFare = seat*price_per_seat;

              return totalFare;



       public void acceleration(){

              //create logic to calculate acceleration which

              //instance of vehicle will use




Source: Vehicle Type


public class VehicleType {

       public static void main(String[] args) {

              Vehicle car = new Vehicle();     //creating car from vehicle blueprint

              car.model = "car1190";
              car.seats = 6;
              car.maxSpeed = 150;
              car.price_per_seat = 50;
              int total_car_fare = car.fareCollection(car.seats, car.price_per_seat);             

              System.out.println("Total collection of car model "+ car.model +" per trip : "+ total_car_fare);
              Vehicle bus = new Vehicle();     //creating bus from vehicle blueprint
              bus.model = "bus123";
              bus.seats = 45;
              bus.maxSpeed = 60;
              bus.price_per_seat = 10;
              int total_bus_fare = bus.fareCollection(bus.seats, bus.price_per_seat);

              System.out.println("Total collection of bus model "+ bus.model +" per trip : "+ total_bus_fare);




You can see here, how the two different instances (car & bus) use same template Vehicle but yet have different state and behavior.

The fields maxSpeed, model, seats, price_per_seat represents object state and methods fareCollection() and acceleration()  define behavior and the way individual object interacts with outside world.


Important point: Depending on the state of each instance the behavior changes.


As per good programming practice class name should start with uppercase letter and object name with lower case letter.

Please Share this page
Views : 319
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!