Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 2 jgs Homework 01 § See description on Canvas. It is all about inheritance, abstract (class and method), and polymorphism. § Due date: You have a week to accommodate to your needs, but you do not need a full week to complete it. No extensions

Slide 3

Slide 3 text

jgs Polymorphism

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs Polymorphism § A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. § Polymorphic references are resolved at run-time, not during compilation.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs Polymorphism

Slide 9

Slide 9 text

jgs Test Yourselves I

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs Shape public abstract class Shape { protected double size; public Shape (double s) {size = s;} public abstract double area(); }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs Square public class Square extends Shape { public Square (double s) { super(s); System.out.println("A Square is born"); } public double area() { return size * size;} }

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs Circle public class Circle extends Shape { public Circle (double s) { super(s); System.out.println("A Circle is born"); } public double area() { return Math.PI * size * size;} }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14 jgs Triangle public class Triangle extends Shape { public Triangle (double s) { super(s); System.out.println("A Triangle is born"); } public double area() { return size * size / 2;} }

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs Main public class Main { public static void main(String[] a) { Shape shape1, shape2, shape3; shape1 = new Triangle(5); System.out.println(shape1.area()); shape2 = new Square(5); System.out.println(shape2.area()); shape3 = new Circle(5); System.out.println(shape3.area()); } }

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs Output

Slide 17

Slide 17 text

jgs Object Class

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18 jgs The Class Object § The Object class is a special class defined in the java.lang package. § All classes are derived from the Object class.

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19 jgs Conversions

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20 jgs getClass

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 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.