Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Inheritance

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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)

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5 jgs Classes

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6 jgs Classes are Better with a Family

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 jgs Classes are Better with a Family

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs Classes are Better with a Family

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs Protected

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs Classes are Better with a Family

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs Classes are Better with a Family (UML)

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

jgs Inheritance Constructors

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs Person

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17 jgs Student and Professor

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18 jgs Main What is the output?

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19 jgs Main

Slide 20

Slide 20 text

jgs To be Continued…

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21 jgs Questions

Slide 22

Slide 22 text

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.