jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 06: Inheritance I Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3 jgs Inheritance § Instead of repeating similar data/method definitions in several classes, we can define one class with the common instance data/methods, and let other classes inherit (extend) these data/method definitions. § Java: parent (super) class and child (sub) class § C++: base class and derived class § New class inherits existing members (variables and functions), add members, and redefine members
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs Single Inheritance § Java supports single inheritance only, i.e., A child class can have only one parent class (direct parent, not ancestors)
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs Protected § Child classes do not inherit private variables § Protected variables and methods can be inherited by child classes, but not as accessible as public § If you plan to have a class inherited by other classes, its data should be declared as protected
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs Note In the Class Diagram: § Parameters are optional § Inherited members are implicit, just like with the source code
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs Inheritance § Constructors are not inherited § A child’s constructor is responsible for calling its parent’s constructor. § Keyword: super
jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2022 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.