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 II
(202109)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 16, 2021
Tweet

Transcript

  1. jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 06:

    Inheritance II Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Previously…

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

    UML Class Diagram # name:String + Vehicle() + Car() + - #
  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs

    Object Oriented Relationships Directed Association Aggregation Generalization Realization Warning! Global Only
  5. jgs Inheritance Constructors

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

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

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

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

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

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

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

    Test Yourselves
  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs

    super # name:String + Vehicle() + Car()
  14. jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14

    class Car extends Vehicle{ public Car(String n) { super (n); } } class Vehicle { protected String name; public Vehicle(String n) { name = n; } } super
  15. jgs Inheritance Override and Overload

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

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

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

    Method Overloading § The process of using the same name for multiple methods with different number or type of the parameters 19
  20. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20 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; } 20
  21. jgs Inheritance Abstract Classes and Interfaces

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

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

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

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

    Abstract Classes
  26. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 26 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.
  27. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 27 jgs

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

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

    Interfaces § Interfaces contains behaviors that a class implements. § An interface cannot be instantiated § An interface does not contain any constructors. § In an interface, all methods are abstract. § An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. § An interface is not extended by a class; it is implemented by a class. § An interface can extend multiple interfaces.
  30. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 30 jgs

    Interfaces
  31. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 31 jgs

    Interfaces
  32. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 32 jgs

    Interfaces
  33. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 33 jgs

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

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