$30 off During Our Annual Pro Sale. View Details »

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 javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Inheritance

  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
  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)
  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5 jgs

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

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

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

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

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

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

    Classes are Better with a Family (UML)
  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
  14. jgs Inheritance Constructors

  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
  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs

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

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

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

    Main
  20. jgs To be Continued…

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

    Questions
  22. jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez,

    Ph.D. javiergs@asu.edu 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.