In this tutorial we’re going to learn about inheritance and job. And for this one let’s start a brand new project by right clicking an Apache Explorer selecting a new project and will name the entire project inheritance and finish.

Now inside of our inheritance project right click the source folder and select new class or create a class and call it parent and click Finish.
And now right click the default package select new class and create a class called child and click Finish.
Now inheritance and object oriented programming is somewhat similar to thinking about inheritance between a parent and a child. For example a child might inherit their eye color from a parent.
The general idea being that a child can inherit some characteristics of their parent.
So let’s see how that would look in Java if we go to our parent class, let’s give it a variable of type string and call it color and we’ll set it equal to Brown. And now our goal is to have our child class inherit this color from the parent.
So how can we do that.
Well if we go over to child there’s a keyword in Java called extends and you do that at the class level.
So we will say public class child and then type the word extends and then parent and now the child class is inheriting the attributes and methods inside the parent class.
And to prove to you that’s the case that’s inside the child class create a main method by taping main
and pressing control spacebar hitting enter and we’ll create a new instance of child say Child c = new child () ;and now will do.
Syso ctrl-spacebar print out c.eyecolor.
And now if we run the application :

we’ll see that Brown is printed out to the console. Now I realized that this Eyecolor variable is nowhere to be found inside the child class but because child now extends parent it automatically inherits the variables and methods that are inside the parent
class.
Now if we go back over to parent we can see that parent doesn’t extend anything but that’s not necessarily true.
Every class in Java automatically inherits the class called object and I can prove that to you by opening up the code in here by pressing Ctrl-spacebar.
And if we look at some of the options here it gives us there is a method called clone, a method called equals, a method called finalize, hash code and to string.

And if you look to the right it says override method in object. What that means is these methods are coming from the class called object.
And essentially we get them for free now and later videos will go into a bit more detail as to what it means to override a method. But for now I just want you to be familiar with what inheritance is. Now when you’re dealing with inheritance there is a one way relationship between the parent and child and to see what that means and we’re going
to draw a little diagram.So let’s say this circle is our parent, and now let’s say we create our child class over here we’ll call it child.
Any of the variables or methods inside a parent will be inherited by a child.
And what that means is that child will inherit all of the functionality of the parent.
And then you could also define additional functionality inside the child.
So for all intents and purposes you can treat a child as if it were a parent. So what we can do here is move this child inside a parent from what we have here is called an is a relationship; in other words a child is also a parent.

and this is where the terminology I used might get a little confusing because in the real world we don’t think of it that way.
But if you think of it instead as a grouping it makes more sense.
For example what if instead of parent and child, we call these groups mammal and dog.
And now there is a relationship is a little more clear because we can say a dog is a mammal but the relationship is one way because the reverse isn’t necessarily true.

Now you might be wondering if the relationship doesn’t really make sense when you’re thinking about a parent and a child in the real world. Why did I start off using that example. Well I just thought it would be the gentlest introduction to the idea of inheritance in general using the example of a parent and a child inheriting the color from the parent.
Now in general in programming it’s common to hear of the the base class this outer class are referred to as the parent and the inner class referred to as the child. There are other terms that are used to describe the relationship as well. For example this could be referred to as the superclass. And this could be the subclass or this could be the base class and this could be the derived class.
And I thought all of those were a little abstract for our first time around.
Now that we have a general idea of what inheritance does and how we might be able to use it. The question you might be asking is why.
The why will become more clear as we learn more about inheritance and the things we can do with it. But one thing I can show you right now is what don’t we create a third class called subclass and we can say it extends parent.
Now we have two classes extending the same class. And this is one of the major advantages that you get of out of using inheritance and that is code reuse. In other words you only have this code in one place in your program but it is used in several other
places by several other classes.
So if you ever need to make changes you only need to make that change in one place.
And you know exactly where it is, it’s inside the parent class that is common to both the subclasses.
Now as one final example just in case my terminology got you confused. That’s creating a new class entirely different from what we’ve been doing so far. And let’s call it myWindow. Now if you’ve watched my earlier tutorials one of them was creating a window using the jFrame class. Now if I remember correctly for that tutorial we did not use inheritance but in this one I’ll show you how you could create the window using inheritance.
So now we have public class my window and I will make this class extend jFrame. Now let’s create a main method to create my window which will call the constructor for this class and will create the constructor by pressing control spacebar and creating the default constructor. And now remember what we’ve learned about inheritance.
My Window extends jFrame. It means my window for all intents and purposes is a jFrame and we can treat it as such which would mean it automatically inherits all the methods and attributes inside the JFrame class.
So if we were to type control spacebar to bring up code in and you’ll see we have a lot of options all of a sudden whereas if we go back to our parent class and press space spacebar there really aren’t very many options at all.
So all these extra options are coming from the fact that we’re inheriting jFrame so some of the options are we could set the size. In other words we’re setting the size of our JFrame window. We can say 500 by 500 and we can set the visibility to true and we can set the title to myWindow.
And now we run the application will see that the jFrame window pops up.

So hopefully that was a gentle introduction to inheritance. And if you don’t get it quite yet don’t worry because there is a lot more to learn about inheritance.
Thanks for watching.