Skip to main content

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.
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.
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.
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.
public class Example {
public static void main(String[] args) {
Bob bob = new Bob();
John john = new John();;

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.


Popular posts from this blog

How Big Data Analytics Can Help You Improve And Grow Your Business?

Big Data Analytics There are certain problems that can only solve through big data. Here we discuss the field big data as "Big Data Analytics". The big data came into the picture we never thought how commodity hardware is used to store and manage the data which is reliable and feasible as compared to the costly sources. Now let us discuss a few examples of how big data analytics is useful nowadays. When you go to websites like Amazon, Youtube, Netflix, and any other websites actually they will provide some field in which recommend some product, videos, movies, and some songs for you. What do you think about how they do it? Basically what kind of data they generated on these kind websites. They make sure to analyze properly. The data generated is not small it is actually big data. Now they analysis these big data they make sure whatever you like and whatever you are the preferences accordingly they generate recommendations for you. If you go to Youtube you have noticed it kn…

How Computers Understand Human Language?

How Computers Understand Human Language? Natural languages are the languages that we speak and understand, containing large diverse vocabulary. Various words have several different meanings, speakers with different accents and all sorts of interesting word play. But for the most part human can roll right through these challenges. The skillful use of language is a major part what makes us human and for this reason the desire for computers that understand or speak human language has been around since they were first conceived. This led to the creation of natural language processing or NLP.
Natural Language Processing is a disciplinary field combining computer science and linguistics. There is an infinite number of ways to arrange words in a sentence. We can't give computers a dictionary of all possible sentences to help them understand what humans are blabbing on about. So, an early and fundamental NLP problem was deconstructing sentences into small pieces which could be more easily…

The Limits of Artificial Intelligence

If you are here, it means that you are familiar with term artificial intelligence. Either you have read about it in school or have seen it in sci-fi movies or somewhere else. Talking about the limitations of AI, let me ask you one simple question first, do you know the definition of AI? You might be thinking to answer me with a yes, yes I know what is artificial intelligence. But what if I tell you that AI is a buzzword and it is almost impossible to properly define. It is this way because the definition of artificial intelligence is moving. People don’t call the things AI that they used to call. For example, a problem that seemed too complex to be solved by human and was solved by AI algorithm is no longer a problem of AI. Playing chess, is one of the examples. It was considered the peek level of artificial intelligence back in previous century. Now it hardly fits the criteria for AI. It is presented to the world as a super power that when given to a computer, it magically starts li…