CHAPTER 4. JAVA BASICS
Methods are also important to share code among developers. If we can find a ready-coded method on the Internet, we can utilize it in our apps easily. Methods are always parts of classes. So, let‟s now focus on classes, objects and inheritance that are the backbones of the so-called “objectoriented programming”.
4.7. Classes, Objects and Inheritance in Java 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
carColour,
String
carFuelType,
float 79