Transcript from the "Inheritance" Lesson
>> So there are four main principles to object oriented programming, we talked about encapsulation already and now we're going to talk about inheritance. So, inheritance is where one class inherits the members of another class. So there are two parties that are involved in an inheritance relationship, a parent and a child.
[00:00:27] So the parent is known as the super class, sometimes it's also referred to as a parent class or a base class. In this example, person is our superclass and then the child is known as the subclass, this is sometimes referred to as a subclass or a derived class or a child class, sorry, child class or derived class.
[00:00:58] And in this example, the employee is the subclass, all right? So an inheritance relationship is known as an is a relationship, an employee is a person, right? And being a subclass is a specialized type of person, so let's demonstrate inheritance with code. We're gonna create a Person class and then we're gonna create an Employee class that inherits from that Person class.
[00:01:36] So within our ID, we'll create a new package, we'll call this one inheritance. Inside of here we'll go ahead and create a Person class. Now if we think about some attributes that a person has, we can come up with things like a name, maybe an age, right? And then let's say a gender.
[00:02:20] And we can go ahead and generate the getter and setter methods for this as well. Again to generate you would right click, and then choose Generate and then Getter and Setter, and select all of the fields, all right. So we have a POJO for a person, now let's create another class, make sure you all have this one, you got it?
[00:02:50] Alright, so let's create another class also under inheritance, this one we'll call Employee. And we want the Employee class to inherit from the Person class. So to inherit from another class, within the class's header, you would specify that this class extends, extends is the keyword that forms an inheritance relationship.
[00:03:28] And then you would specify what class this inherits from, so we can write Person and that's it, that's all you need to create this inheritance relationship between these two classes. Now everything that is not private within the Person class, now also belongs to the Employee class, it has inherited these things from it.
[00:04:03] So, you can inherit from a superclass, but then remember the subclass is some specialized version of that class. So you might have things that are specific just to an employee, so we can add anything we want to this. So we get whatever that we inherited from our superclass and then whatever we define for our sales.
[00:04:27] So, some attributes for an employee might be an employeeId. And maybe a title. We could go ahead and create the getters and setters, so we'll generate those. And great, now we have employee inherited from person and then also a couple of attributes and methods for itself. Now I want us to test this out, so we'll create one more class, and we'll call this one InheritanceTester.
[00:05:24] In InheritanceTester, let's create a main method and we'll go ahead and instantiate the Person class. So we'll say person = new Person. I have not created any constructors, but notice I'm able to call to this default constructor, why?
>> Ghost constructor.
>> Yeah [LAUGH] I think I made that term up about ghost constructor [LAUGH], but yes, because Java has implicit default constructor if you have not defined any constructors.
[00:06:05] Okay, so we have the Person, right, and then let's go ahead and make an Employee object. We'll call him employee = new Employee. Okay, now notice, if I click, if I say employee, well, lemme show you this. If I say person dot, the methods that are available to this person are getAge, getGender, getName, setAge, Gender, and Name, all right?
[00:06:44] So, if we look in person, I think I can split this [COUGH]. Okay, nice, okay, so in the person, this is the Person class on the right, we have the name, the age, the gender and in our InheritanceTester, of course, we can get those things with person. But if we look at the employee object and say, doc, we have get and set the EmployeeId, get and set the Title, which are the things that were specific to Employee class.
[00:07:23] But we also have the GetAge, the Gender, and the Name, which are the methods that were inherited from the Person class. Does everyone understand, any questions so far? Okay, beautiful, all right, so. Within inheritance constructors operate a little bit differently, the constructors in the superclasses are going to be executed before the constructors in the subclass, all right?
[00:08:09] So, it makes sense if you think about it, Java wants to make sure that before you inherit something and start executing it, those things are properly constructed, right? So it's gonna run the superclass first and then the subclass. Let's explore constructors by adding some to the Person and the Employee classes, all right?
[00:08:37] So back to our Id, I'm gonna go inside of my Person class Sorry. Okay, I'm gonna go inside of Person and let's go ahead and create a default constructor here. So default constructor, someone give me the syntax. First word.
>> Public, next word.
>> Person, the exact name of the class, very good, no arguments in the default constructor and we'll just put a print statement cuz I want you to see where we are when we execute this.
[00:09:33] So we'll just say In Person default constructor, all right? And then let's go to the Employee class and we'll add a constructor there, so public Employee And we'll print out In Employee default constructor. All right, now, in our InheritanceTester we'll just run this, so I want you to see the order that the constructors run.
[00:10:24] So I'm going to actually, let's just do one at a time. So let's run the Person instantiation first, and of course we should expect to see that it is in the Person default constructor, yes, now I'm gonna comment out that one and run the Employee one. So if we run Employee, notice it ran the Person one first, and then the Employee, okay?
[00:11:02] Now we're gonna make another constructor inside of Person and this constructor will take a name, so let's create the second one and this takes a String name. And we'll just print out that we're in In Person 2nd constructor. All right? Now, in the Employee class. If I wanted to.
[00:11:59] Not have the default constructor run, cuz by default, this Employee constructor is gonna run the default one from the Person constructor. But what if I explicitly want to run one of the other constructors from the Person class? Then I would have to specify that myself in my constructor as the subclass.
[00:12:24] All right and to do that, you make a call to super All right, so super will call the constructor that matches the parameter list, so in this case, I have no parameters, this will explicitly call the default constructor from the Person class. Now, I was gonna do that anyway, so this is not necessary, but if I wanted to call that second constructor in person, I would do so by passing in a name.
[00:13:00] So you can type in your name there, and this will call the constructor that takes a string in the Parent class, okay? All right, so let's run our InheritanceTester with Employee just to make sure we understand what's going on. So it went to the second constructor of person first because that's what we specified to do and then it implemented anything else.
[00:13:35] Now, if you're going to make an explicit call to super, it has to be the very first line of the subclass's constructor. For example, I could not move super to after here, I would get an error and that makes sense, right? The parents stayed in everything needs to be set up first before you can start doing things in the subclass.
[00:14:10] Does that make sense to everyone? Okay. The other thing is if, let's comment this out for a second, and let's go back to Person and we're gonna comment out that default constructor. Now, if there is no default constructor, then notice now I get an error in my subclasses default constructor.
[00:14:42] Because it wants to make the call to that default constructor from Person whether I explicitly defined it or not. And since I added a second constructor that ghost's default constructor went away remember. So there is no constructor now, no default constructor that Employee could call, so I have to explicitly make a call to a constructor, okay?
[00:15:20] Any questions on this? All right.
>> Can a class have more than one parent?
>> No, so you can only inherit from one class, one parent, okay? All right, so, key points to remember about constructors when we're talking about inheritance. A superclass's constructor is run before the subclasses constructor, explicit calls can be made to the superclasses constructor from one of the subclasses constructor by using super.
[00:16:07] You'll pass in any arguments you want to get that right constructor, if there is an explicit call to the superclasses constructor, it must be the first statement in the subclasses constructor. And if the superclass doesn't have a default constructor, the subclass must explicitly call one of the other constructors.