Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously…

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

jgs Test Yourselves super

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

jgs Inheritance Override and Overload

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

jgs Inheritance Abstract Classes and Interfaces

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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.