1 / 42

COP 3503 FALL 2012 Shayan Javed Lecture 5

COP 3503 FALL 2012 Shayan Javed Lecture 5. Programming Fundamentals using Java. Abstract Classes. Abstract classes. From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected float area; // Constructors… // get/set methods…

zudora
Download Presentation

COP 3503 FALL 2012 Shayan Javed Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 FALL 2012ShayanJavedLecture 5 Programming Fundamentals using Java

  2. Abstract Classes

  3. Abstract classes • From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected float area; // Constructors… // get/set methods… }

  4. Abstract classes • So we can do: GeometricObjectgObj = newGeometricObject(“AnObject, Red”);

  5. Abstract classes • So we can do: GeometricObjectgObj = newGeometricObject(“AnObject, Red”); But does it make sense to do this? What is a “GeometricObject” by itself?

  6. Abstract classes • Solution: • Make it abstract!

  7. Abstract classes • Solution: • Make it abstract! publicabstract class GeometricObject {

  8. Abstract classes • Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.)

  9. Abstract classes • Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.) • Then define “concrete” concepts as subclasses. (Circle, Rectangle, etc.)

  10. Abstract classes • Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.) • Then define “concrete” concepts as subclasses. (Circle, Rectangle, etc.) • More strictness = less room for ambiguity/error.

  11. Abstract classes • Every GeometricObject has an area.

  12. Abstract classes • Every GeometricObject has an area. • But getArea() defined differently for concrete objects.

  13. Abstract classes • Every GeometricObject has an area. • But getArea() defined differently for concrete objects. • (Not defined for the GeometricObject class)

  14. Abstract classes // Circle public class Circle extendsGeometricObject { publicfloatgetArea() { return Math.PI * radius * radius; } }

  15. Abstract classes // Circle public class Circle extendsGeometricObject { publicfloatgetArea() { return Math.PI * radius * radius; } } // Rectangle public class Rectangle extendsGeometricObject { publicfloatgetArea() { return width * height; } }

  16. Abstract classes GeometricObject[] objs = newGeometricObject[2]; objs[0] = newCircle(3); objs[1] = newRectangle(1, 2); for (GeometricObjecti : objs) { System.out.println(i.getArea()); }

  17. Abstract classes GeometricObject[] objs = newGeometricObject[2]; objs[0] = newCircle(3); objs[1] = newRectangle(1, 2); for (GeometricObjecti : objs) { System.out.println(i.getArea()); } Will not Compile! Cannot find “getArea()” in the GeometricObject class.

  18. Abstract classes • So make getArea() an abstract method in GeometricObject.

  19. Abstract classes • So make getArea() an abstract method in GeometricObject. • Only a declaration in GeometricObject: publicabstract class GeometricObject { ... publicabstract float getArea(); }

  20. Abstract classes GeometricObject[] objs = newGeometricObject[2]; objs[0] = newCircle(3); objs[1] = newRectangle(1, 2); for (GeometricObjecti : objs) { System.out.println(i.getArea()); } Will now work! getArea() defined in GeometricObject

  21. Abstract class characteristics • Class has abstract methods = has to be an abstract class

  22. Abstract class characteristics • Class has abstract methods = has to be an abstract class • But:Abstract class can have noabstract methods.

  23. Abstract class characteristics • Class has abstract methods = has to be an abstract class • But:Abstract class can have noabstract methods. • Subclass can be abstract, superclass can be concrete.

  24. Abstract class characteristics • Concrete subclasses must define superclassabstract methods. • Circle, Rectangle have to define their own getArea(); • Abstract methods can be defined, but usually aren’t. • Abstract classes cannot be instantiated.

  25. Abstract class characteristics • Abstract class can be used as a data type though. • Example: GeometricObjectgObj; GeometricObject[] objs = newGeometricObject[2];

  26. Abstract class characteristics • Why is this allowed: GeometricObject[] objs = newGeometricObject[2]; • They can’t be instantiated, so why allow as data types?

  27. Abstract class characteristics GeometricObject[] objs = newGeometricObject[2]; objs[0] = newCircle(3); objs[1] = newRectangle(1, 2); for (GeometricObjecti : objs) { System.out.println(i.getArea()); } Can take advantage of polymorphism!

  28. Abstract classes • Let’s say we need to check if areas of GeometricObjectsare equal

  29. Abstract classes • Let’s say we need to check if areas of GeometricObjectsare equal • Define method called equalArea(...)

  30. Abstract classes publicstatic void main(String[] args) { Circle c1 = newCircle(3.0); Rectangle r1 = newRectangle(2, 3); Rectangle r2 = newRectangle(1, 6); SOP(“c1 and r1 have equal area: “ + equalArea(c, r1)); SOP(“r2 and r1 have equal area: “ + equalArea(r2, r1)); }

  31. Abstract classes • How do you define equalArea(...)?

  32. Abstract classes • How do you define equalArea(...)? publicstaticbooleanequalArea( ? obj1, ? obj2) { return obj1.getArea() == obj2.getArea(); }

  33. Abstract classes • Could define it for all different comparisons: publicstaticbooleanequalArea( Circle c1, Rectangle r1) { return c1.getArea() == r2.getArea(); }

  34. Abstract classes • Could define it for all different comparisons: publicstaticbooleanequalArea( Circle c1, Circle c2) { return c1.getArea() == c2.getArea(); }

  35. Abstract classes • Could define it for all different comparisons: publicstaticbooleanequalArea( Rectangle r1, Rectangle r2) { return r1.getArea() == r2.getArea(); }

  36. Abstract classes • Could define it for all different comparisons: publicstaticbooleanequalArea( Rectangle r1, Rectangle r2) { return r1.getArea() == r2.getArea(); } Tedious...

  37. Abstract classes • Make it general thanks to abstract methods/classes: publicstaticbooleanequalArea( GeometricObject g1,GeometricObject g2) { return g1.getArea() == g2.getArea(); }

  38. Abstract classes • Why not use the Object class? publicstaticbooleanequalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem?

  39. Abstract classes • Why not use the Object class? publicstaticbooleanequalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem? Object class has no getArea() method!

  40. Abstract classes • Why not use the Object class? publicstaticbooleanequalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem? Object class has no getArea() method! How would you fix it?

  41. Abstract classes • Why not use the Object class? publicstaticbooleanequalArea( Object g1, Object g2) { GeometricObject go1 = (GeometricObject)g1; GeometricObject go2 = (GeometricObject)g2; return go1.getArea() == go2.getArea(); } Cast the objects

  42. Next Lecture... • Interfaces.

More Related