Wednesday, 17 May 2017

Tutorial 12: Inheritance



In this tutorial, we discuss the concept of inheritance in Java by starting with a few examples to give you an idea about what we are talking about.


Let’s say that you create a class named Machine. Then, you give the class a String member variables called name and an integer member variable called id. This makes sense because every machine has a name and an id. Also, let us define two methods for this class. One method starts the machine, while the other stops the machine. So the class will look like this:

public class Machine {

            protected String name;

            protected int id;



            public void start () {

                        System.out.println (“Machine is starting”);

            }



            public void stop () {

                        System.out.println (“Machine is stopping”);

            }



            public static void main (String[] args) {

                        Machine machine1 = new Machine ();

                        machine1.start();

                        machine1.stop();

            }
}
Figure 1: The Machine class.


Now, let’s say that we create another class called Vehicle. Now we know that a vehicle is a machine. Every vehicle has a name, which may be its make, and an id, which may be its chassis number. If you think about it, the class Machine and Vehicle have so much in common because a vehicle is a machine. So it would not make sense if we were to start defining the same member variables that are in the Machine class in the Vehicle class. This is the essence of inheritance in Java.


In Java, we show that one class inherits from another class by saying that it extends the class that it inherits from. Why? Again, think about it. A machine is too general a term, right? We may have maize milling machines, calculators, computers and so on. In fact, even the items that Leonardo da Vinci designed were machines. But when we talk specifically of a calculator machine, or a vehicle machine, or a computer machine, we can see that it goes beyond the fundamental definition of what a machine is. This is why we say that Vehicle extends Machine.


In this context, we refer to the Machine class as the parent class and the Vehicle class as the child class. In Java, we indicate that the Vehicle class inherits from the Machine class like this:

public class Vehicle extends Machine {

            protected int cc;

            protected int seatingCapacity;



            public void startVehicleRadio () {

                        System.out.println (“Starting the vehicle’s radio”);

            }



            public void showFuelLeft () {

                        System.out.println (“The tank is full”);

            }



            public static void main (String[] args) {

                        Vehicle vehicle1 = new Vehicle ();

                        vehicle1.name = “Bentley”;

                        vehicle1.id = 550;

                        vehicle1.cc = 6000;

                        vehicle1.seatingCapacity = 2;

                       

                        System.out.println (vehicle1.name);

                        System.out.println(vehicle1.id);

                        System.out.println(vehicle1.cc);

                        System.out.println(vehicle1.seatingCapacity);

                       

                        vehicle1.startVehicleRadio ();

                        vehicle1.showFuelLeft ();

}
}


Figure 2: The Vehicle class extends the Machine class and I can access the member variables that the Vehicle class has inherited from the Machine class.


When we say that Vehicle extends Machine we are saying that the Vehicle class inherits the functions and member variables of the parent class, which in this case is the Machine class. This means that we can give items of the Vehicle class a name and an id. We can also call the methods start() and stop() on items of the Vehicle class.


However, we cannot access the member variables of the Vehicle class from the Machine class. This makes sense because even in real life, a child inherits from his/her parents and not the other way round.


We can create another class that inherits from the child class. In our case, let’s say that we want to create a class called SportsCar. Now, we know that a SportsCar is a Vehicle. So instead of defining the same member variables that are in Vehicle class in the SportsCar class such as cc and seatingCapacity, we can say that SportsCar extends Vehicle

This means that the SportsCar class will inherit from the Vehicle class and the Machine class, because the Vehicle class, which is the parent of the SportsCar class, is also a child of another class, which is the Machine class. This means that the SportsCar class inherits all the functions and member variables of both the Vehicle and the Machine class.


public class SportsCar extends Vehicle {

            private String rally;

            private String driverName;

           

            public static void main (String[] args) {

                        SportsCar sportsCar1 = new SportsCar ();

                        sportsCar1.name = “Herbie”;

                        sportsCar1.id = 53;

                        sportsCar1.cc = 2000;

                        sportsCar1.seatingCapacity = 2;

                        sportsCar1.rally = “Dakar”;

                        sportsCar1.driverName = “Enzo”;

                        System.out.println (“name: ” + sportsCar1.name);

                        System.out.println (“id: ” + sportsCar1.id);

                        System.out.println (“cc: ” + sportsCar1.cc);

                        System.out.println (“seats: ” + sportsCar1.seatingCapacity);

                        System.out.println (“rally: ” + sportsCar1.rally);

                        System.out.println (“diver’s name: ” + sportsCar1.driverName);

                        sportsCar1.start();

                        sportsCar1.stop();

                        sportsCar1.startVehicleRadio();

                        sportsCar1.showFuelLeft();

            }
}


Figure 3: The SportsCar class extends the Vehicle class. The Vehicle class extends the machine class. Hence the SportsCar class can access the public methods and protected member variables of both the Vehicle and the Machine class.


Now notice the use of the keyword protected. In the tutorial on access specifiers, we said that when we label a member variable as protected, it means that it is visible within its child classes. As you can see from the results of running the above program, we are defining the protected member variables of the parent classes in the child class.


Also, I’d like to point out the fact that in this tutorial, I’ve been using the words “is a” a lot. I said that a Vehicle is a Machine and that a SportsCar is a Machine. The words “is a” tell you that the objects have an inheritance relationship.


In Java, there are many different classes. However, if you trace the classes backwards, you will end up with a parent class of all Java classes called the Object class. This is why you saw me writing, for example, that sportsCar1 is an object of the type SportsCar. However, we don’t go about declaring every class we create extends the Object class because this is implied. Java knows that every class extends the Object class implicitly.


It is very important that we understand the Object class, and this will be the basis of the next tutorial. We will look at the Object class and specifically, the methods toString() and equals().


I hope you have enjoyed this tutorial at least as much as I have enjoyed writing it. If you have any questions or comments, please drop them in the comments section below and I will address them.

Until next time, stay safe.

No comments:

Post a Comment