Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 06

CSE205 Lecture 06

Object-Oriented Programming and Data Structures
Inheritance I
(202202)

Javier Gonzalez-Sanchez PRO

September 16, 2021

Transcript

22 slides
  1. 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
  2. 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
  3. 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)
  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6 jgs

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

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

    Classes are Better with a Family
  7. 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
  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs

    Classes are Better with a Family (UML)
  9. 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
  10. 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
  11. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming