Java programming language uses traditional (procedural) and objectoriented concepts together for a stronger experience. Procedural programming means that a program uses procedures and functions executed by order. This is the traditional method. On the other side, object-oriented programming uses classes and objects derived from these classes to execute the required computational steps.
So, what are classes and objects? We can think classes as blueprints and objects as different products made using this blueprint. Similarly, we can consider a car factory as an example for classes and objects. Imagine a car production band. Each time we change the colour, baggage size, steering wheel type, etc., we obtain a different car without changing the basic properties of the car.
If the car make and model are Virtuma and Liberty (hypothetical names!), the production band produces Virtuma Liberty 1.6, Virtuma Liberty 2.0, Virtuma Liberty 3.0, Virtuma Liberty 1.6 premium, Virtuma Liberty 2.0 diesel, etc. In this case the class is Virtuma Liberty and all these hypothetical models are objects belonging to this class. We can imagine this as in Figure 4.26. Let‟s declare a Car class in Java first and then define different car objects derived from this class as in Code 4.31.
In this code, a class named Car is defined by public static class Car which has variables named Colour, fuel type and engine capacity. These are the variables which will be different for each object derived from this class. Inside the class definition, there is a method declaration public Car(String caroler, String car Fuel Type, float.
This is a constructor method having the same name with the class name and the class variables are assigned inside this method by the code lines shown in Code 4.33.