Inheritance In JAVA: Example

img: Inheritance In JAVA: Example
This is the second last section of the topic Inheritance In OOP where we will study some code snippets. This study will clarify the idea of how to achieve inheritance in JAVA and how it actually works. Without wasting any time and giving length to the post for no reason, let’s start rolling.
Prerequisites: To understand these snippets you should already know what inheritance is. If you don’t, don’t worry. I have a blog post, go here.
Overview: I have written a program named Example which depicts real life inheritance properties like a relationship between child and parent, child access to parent properties etc. The program consists of 4 very simple java classes which are extremely easy to understand. However, if you face any issue ping me in comments. I am always there for you. ExampleBobJohn, and Stephen. The bold faced letters are not names of some persons, in fact, these are names of classes which we are going to study here.
In Java, to establish a child parent relation between two classes, the keyword extends is used at the class declaration time. The child class name is written at the left of the keyword and the parent class name is written on the right side. Syntax:
Child_Class_Name extends Parent_Class_Name
Let’s cast a glance over the access modifiers.
Public: members specified with the public keyword are accessible anywhere in the program using the class object.
Protected: Members modified by protected keyword are accessible within its own class and the child classes without using any object of the parent class.
Privateprivate members are available within the scope of only its own class. 

Stephen.java
public class Stephen {
protected int money = 5000;
}

This is the simplest and easiest class I think I have ever seen. As you can see, it only has a single integer data member named money which is initiated to 5000. This class does not extend any class it means that it does not have any parent class. In our story context, Stephen is the grandfather of Bob and have $5000 in his bank account. The variable money is qualified by the keyword protected which means that this variable is accessible in this class and its child classes only. It (variable money) is inaccessible by the object (Object of Stephen Class). Simple.
Bob.java
public class Bob extends Stephen{
protected String eyeColor = "green";
protected String car = "Bob's BMW";

private String mailbox = "Bob's Mailbox";
public void drive() {
System.out.println("Bob is driving: " + car);
}
public void eyeColor() {
System.out.println("Bob's EyeColor: " + eyeColor);
}
public void money() {
System.out.println("Bob says: my dad have $" + money);
}
}

Bob class extends Stephen class (Bob is a child of Stephen). It has two protected String variables, eyeColor and car and one private String variable, mailbox. What does it show? It shows that the child classes can have or use the eyeColor and car properties while they can't use the mailbox variable. In real life, a child can inherit eye color from his/her parent and can also use their parent’s car but they can’t read their parent’s mailbox unless they are allowed to. While writing this example, I tried to be as real as I could because I think it makes it easier to understand. There are three simple public functions which are accessible through the class object. These functions are self-explanatory.
John.java
public class John extends Bob{
public void eyeColor() {
System.out.println("John's eyecolor: " + eyeColor); // note the eyeColor is from 'Bob' Class
}
public void drive() {
System.out.println("John is driving: " + car); // car is also from Bob class
}
public void money() {
System.out.println("John says: My grandpa have $" + money); // money is from Stephen class
}
}

Class John has nothing more than three functions which simply prints some string with some variables (eyeColor, car, money) from its parent classes. You can see that John is a child of Bob and can access private and public members of Bob class. However, it is using money variable from Stephen class which it does not extends. What the heck is this? Is this correct? Well, yeah! It is correct. The thing is John extends Bob and Bob extends Stephen class so John is also a child of Stephen. Kind of grandchild. This is called multi-level inheritance.

Example.java
public class Example {
public static void main(String[] args) {
Bob bob = new Bob();
John john = new John();

bob.drive();
bob.eyeColor();
bob.money();
john.drive();
john.eyeColor();
john.money();
}
}

Output: 
Bob is driving: Bob's BMW
Bob's EyeColor: green
Bob says: my dad have $5000
John is driving: Bob's BMW
John's eyecolor: green
John says: My grandpa have $5000

This is the final main class which includes the main function. It simply creates objects of Bob and John classes and calls some of their public functions. Easy. Look at the output and figure out how does it works. It is your home task. If you find yourself in trouble, contact me via comments below.
In coming section, we will discuss why did we study inheritance? And where to use it.  Subscribe using the subscribe button up there in the toolbar and we will notify you when the post is ready. If you have any questions or suggestions, please leave in the comments below and also mark one of the options in the survey popup. 
Stay tuned.

Comments