What is Class?

A class is simply a representation of a type of object. It is the blueprint/plan/template that describes the details of an object.

For example, here we have a class Car that has three data members (also known as fields, instance variables and object states). This is just a blueprint, it does not represent any Car, however using this we can create Car objects (or instances) that represents the cars. We have created two objects in CarApplication, while creating objects we provided separate properties to the objects using constructor.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class Car {
    private int id;
    private String name;
    private int year;

    public Car(int id, String name, int year) {
        this.id = id;
        this.name = name;
        this.year = year;
    }

}

class CarApplication{
    public static void main(String[] args) {
        Car vega = new Car(1, "Vega", 2020);
        Car tesla = new Car(2, "Tesla", 2018);
    }
}

Comments

Popular Posts