Java Basics

Java Basics 13: Heritage

14 videos

80 minutes

Once seen objects, let's see what is the inheritance, the inheritance is to create objects that have the same characteristics of objects parents, plus other different, is equal to what happens in real life, so for example, I have created the class and train this train has machinist, you have old, has wagons with passengers, perhaps, if I now think a luxury train, which has a bar, a waiter. Well then what do I do? I have to

create again, all the characteristics of the trains because the luxury train also has machinist, wagons, also has seniority? And I have to create me two different class? Do not! ... I just think the train class and then, the luxury class that receives all train, plus what you have extra, that is, heritage, ok! To save repetition code, which is common, but rather inherited from one another, for the same properties and the same

methods, all this, all you have inherited the parent class. So, let's do that, let's create us here a class, here I put the class 'luxury' and in this class we will say you have to inherit all train, how I do that ... I put it here? 'extends train' train runs, ok! And here in luxury, you also give its own characteristics, for example we will create a String with the name of the waiter, and we will create us a variable, Boolean, where

We have here the bar, and here we will save if it is open or closed, right? What does this mean? Again here that I think the builder class luxury, so I'll put the public fancy, and we will tell you that if no information is given, the server is anonymous, and we will say that the bar is closed, we, false, this means that the bar is closed, or else we can do is create our constructor, where here we pass the data, this will

receiving a String with the name of the server and will receive a Boolean, with the status bar, then here the waiter can we pass the name, which We have given and bar will let pass the state to tell us, whether open or closed .... Ok! ... Let's save, and now here, in all this we are doing I'll delete line ... so I'll just leave this, to see the details here, and now I'll create another train here, but this is luxury, will be the

third train and here I put, new luxury, ok! Well if I show here, they'll see here, at the very end, we will say that we show, for example, name waiter, bartender, waiter's name will be anonymous, if you step luxury here the waiter, and I say, then, 'Felipe', and let's say the bar is open, and here we put a, true, now when I throw, here tells me that the waiter is called Felipe, did you see? and so that I can make

the characteristics of here, luxury features can also make more before the train, so I now can consult for example, the capacity will have that train, in that state, she tells me that zero, see? And here also, you can get passengers, here the third, also I can you meter passengers, because all methods are inherited first train, you see? ... the third've got him five in the wagon with one and tells me

the reservation is completed, because the same functions we created for the parent class, have been inherited by the son, but other than its own which I may have, I here, I can create your own class, read: 'StatusBar', status bar, and here we are going to pass a variable, Boolean, in the state and we to open or close it, as we want, then we'll put that bar = state, and here we asked, if (bar = = true), we will show

per screen, the bar has been opened, the bar is open, and if not we will show on screen, the bar is closed, so now here, as I can, to train third number, which was luxurious to say: StatusBar, and pass a, false, and now I tell you that the bar has been closed, forgive ... StatusBar, to see, see if I wrote something wrong ... I'm going to copy me this case, have not written this good! ... If Ah yes, no! It is that this is not a message

screen. I have to invoke it simply ... it is now! ... Now, the bar is closed, and if I say, true, the bar is open, you see? But what happens if I I want to do this the first train? I can not, It gives me error! Because the first is a normal train, does not inherit luxury, therefore these characteristics only they will have, trains that have been the train class, but train will not receive the luxury train will not be able to have any of this, however,

luxury trains, if they will have their own, more from the rest because inherits luxury train, ok! So, I choose to take things a father, but the father does not have son things, this is how inheritance in Java, well, then, you've seen something very powerful, you've seen how to create objects, which can represent situations of reality with its own characteristics, and able to do things themselves, and how to make others inherit from them and receive the same more

likewise, good! This has been a very powerful lesson and for any object-oriented language that is essential to master it. Once we seen this to see another video over Java.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.