CSE 14 Lecture Notes - Lecture 19: Abstract Type, Access Modifiers, Semicolon
CMPS12A Lecture 19- Inheritance
If you did’t kow already, lasses all oe fro the Ojet lass itself. The Perso lass that
we made in earlier lectures or the Scanner, even String! They are all descendants from the
Object class which is default in Java.
If one wanted to create a subclass of a class that you made, for example the Person class, one
would need to use extends. One would make another file and do something like
class Alien extends Person {
//then the rest of the code here
}
Thus, one way to look at what class is the subclass of what, we first find the main class itself,
which is Object right.
Object - > Person -> Alien
where Person is the super class of base class and Alien is the subclass. By super class, it means
that it is the class we made, which can be considered like the ai as well if we do’t thik of
Object.
What is the benefit of making a subclass to extend our main class itself? Well, having the
subclass now, we inherit the fields, methods, and nested classes of the super class (Parent in
this case) that we can happily use for different things. Now, using these classes, always
remember that the subclass can use the super class but the super class can not use the sub
class, whih akes sese right. If it does’t thik of it like this. Assume we already make a
Person class and we extended the Person class with Alien class.
Example:
Person a = new Person();
Alien b = new Alien();
a Person object
Person part of Alien
b Alien object
Since Alien has everything a Person has,
find more resources at oneclass.com
find more resources at oneclass.com