Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 08

CSE205 Lecture 08

Object-Oriented Programming and Data Structures
Polymorphism II
(202109)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 18, 2021
Tweet

Transcript

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

    Polymorphism 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

    Scenario
  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs

    Shape public abstract class Shape { protected double size; public Shape (double s) {size = s;} public abstract double area(); }
  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5 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;} }
  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6 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;} }
  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 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;} }
  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 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()); } }
  9. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs

    Output
  10. jgs Object Class

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

    Conversions
  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs

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

  15. jgs Test Yourselves Polymorphism

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

    Test Yourselves
  17. jgs Next GUI

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

    Next... GUI
  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 22 jgs

    Questions
  20. 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.