Transcript from the "Chain of Inheritance" Lesson
>> Is there any level, other than practicality, for how deep that you can set up the parent and inherited relationship, trying to think of an example. Let's say you had a overarching primary class, say, Car. And then what you do is you then, say, have a class that inherits from Car, like, I don't know, Ford.
[00:00:24] Then from there you might have a specific model of that class. And the hierarchical relationship, is there any limit to what you could be inheriting, in terms of layers?
>> Right, so that's what we are getting into now, the chain of inheritance. And to answer your question, there is no limit, well, no, not by default.
[00:00:49] There's some things you can do to kind of limit stuff. But by default, you could have, let's say, 50 classes, where one inherits from another, that inherits from another, that inherits from another, and so on and so on, all right? So in Java, a class can only explicitly inherit from one other class.
[00:01:12] So when we say that extends, we could only put one class there, you can't put multiple ones. But that superclass can also inherit from another class. So if I'm a subclass, and I inherit from class A, let's say, class B. I'm class C, I inherit from class B, and class B inherits from class A.
[00:01:40] As C, A is my grandparent, and I've inherited their stuff too, so I've inherited from A and from B, all right? By default, and I talked briefly about this earlier, but the Object class is a class within Java that I said is the godfather of all other classes.
[00:02:06] So every class inherits from the Object class. So remember, you may have seen sometimes, when I say, let's say, square dot. I see all of the classes from square, I see all of the classes from rectangle, but under that, I also see all of the methods. I also see some more methods, equals, hashCode, toString, getClass.
[00:02:35] All of these were methods that I have inherited from the Object class. So you'll see those on any object if you did a dot, all right? So we're going to look at an example here, where we have a Vehicle class with fields for color and mileage. And then it also has some getters and setters for those fields.
[00:03:05] And then we also have a Car class, which inherits from Vehicle, right? And it has its own field for doors, and getters and setters for those. And then we have a Coupe class that extends from Car, and it just sets the number of doors to 2. No fields of its own.
[00:03:36] And then we have a Garage class. And in Garage, we have a Coupe object that we can set the color for, even though setColor is not in the Coupe class, right? setColor is also not in the Car class that Coupe extends from, setColor is in the Vehicle class.
[00:04:02] So you see here that it inherited that through the inheritance from its superclass. Does this make sense? Any questions?
>> I don't know why you'd want to do this, or it's probably not possible, maybe it is. Can you reverse it? Can like the grandfather inherit anything from the child?
>> No, good question. So a parent or any ancestor in the tree does not inherit anything from its children. Just like if you think about your parents, they inherited nothing from you, right? They already existed, you inherited from them. Also, so if we had this Coupe class that extends Car, what's another kind of car?
[00:04:59] I don't know anything about cars.
>> Convertible, that also extends Car. Coupe and Convertible are not siblings, they have no relationship with each other, okay? So don't think that they can do anything special because they have the same parent, they do not.
>> And if you wanted one to be related to another, you'd have to be making it a bottom level of one of the classes, instead of-
>> There's no sibling, there's nothing you can do to make them siblings, there's no concept of such. The only sort of relationship you could do is a hierarchical one.
>> That's what I'm saying, yeah, okay. Is there any Java-specific term for two classes that inherit from the same parent?
>> Nope, doesn't exist.
>> Love it.
>> [LAUGH] Okay, subclass one and subclass two or something like that.