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

CSE205 Lecture 07a

CSE205 Lecture 07a

Object-Oriented Programming and Data Structures
Polymorphism
(202109)

Javier Gonzalez-Sanchez
PRO

September 17, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 205
    Object-Oriented Programming and
    Data Structures
    Lecture 07: Inheritance II
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Previously…

    View Slide

  3. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3
    jgs
    Inheritance
    § Constructors are not inherited
    § A child’s constructor is responsible for calling its parent’s constructor.
    § Keyword: super

    View Slide

  4. jgs
    Test Yourselves
    super

    View Slide

  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5
    jgs
    Case 1
    # name:String
    + Vehicle()
    + Car()

    View Slide

  6. jgs
    Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6
    class Car extends Vehicle{
    public Car(String n) {
    super (n);
    }
    }
    class Vehicle {
    protected String name;
    public Vehicle(String n) {
    name = n;
    }
    }
    Case 1

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11
    jgs
    Case 2: Make this work

    View Slide

  12. jgs
    Inheritance
    Override and Overload

    View Slide

  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13
    jgs
    Method Overriding
    § A child class can override (redefine) the definition of an inherited method.
    (The new method must have the same signature.)
    § A method with the final modifier cannot be overridden.
    Warning:
    § Applied to variables final modifier makes them constant.

    View Slide

  14. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14
    jgs
    final

    View Slide

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15
    jgs
    final

    View Slide

  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16
    jgs
    Method Overloading
    § The process of using the same name for multiple methods with different
    number or type of the parameters
    16

    View Slide

  17. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17
    jgs
    Method Overloading
    public int calculate (int num1, int num2) {
    int sum = num1 + num2;
    return sum;
    }
    public int calculate (int num1, int num2, int num3) {
    int sum = num1 + num2 + num3;
    return sum;
    }
    17

    View Slide

  18. jgs
    Inheritance
    Abstract Classes and Interfaces

    View Slide

  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19
    jgs
    We do not want to allow this

    View Slide

  20. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20
    jgs
    Abstract Classes
    § cannot be instantiated and contains one or more abstract methods (no
    definition body).
    § can contain methods
    § can contain data declarations

    View Slide

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

    View Slide

  22. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 22
    jgs
    Abstract Classes

    View Slide

  23. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 23
    jgs
    Abstract Methods
    § An abstract method is a method without a body.
    § It cannot be final or static because there is no definition, and it should be
    changed later.

    View Slide

  24. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 24
    jgs
    Abstract Methods

    View Slide

  25. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 25
    jgs
    Abstract Methods

    View Slide

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

    View Slide

  27. jgs
    CSE 205 Object-Oriented Programming and Data Structures
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    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