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
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  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

    View Slide

  2. jgs
    Inheritance

    View Slide

  3. 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

    View Slide

  4. 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)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  13. 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

    View Slide

  14. jgs
    Inheritance
    Constructors

    View Slide

  15. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. jgs
    To be Continued…

    View Slide

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

    View Slide

  22. 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.

    View Slide