CSE 14 Lecture Notes - Lecture 19: Abstract Type, Access Modifiers, Semicolon

43 views3 pages
10 Jun 2018
School
Course
Professor
CMPS12A Lecture 19- Inheritance
If you did’t kow already, lasses all oe fro the Ojet 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 thik 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, whih akes sese right. If it does’t thik 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
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents