cop 3503 fall 2012 shayan javed lecture 5

42
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1

Upload: zudora

Post on 24-Feb-2016

45 views

Category:

Documents


0 download

DESCRIPTION

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… - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 5

1 / 41

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 5

Programming Fundamentals using Java

1

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 5

2 / 41

Abstract Classes

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 5

3 / 41

Abstract classes

From the previous lecture:

public class GeometricObject {protected String Color;protected String name;protected float area;

// Constructors…

// get/set methods…}

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 5

4 / 41

Abstract classes

So we can do:

GeometricObject gObj = new GeometricObject(“AnObject, Red”);

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 5

5 / 41

Abstract classes

So we can do:

GeometricObject gObj = new GeometricObject(“AnObject, Red”);

But does it make sense to do this?

What is a “GeometricObject” by itself?

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 5

6 / 41

Abstract classes

Solution:

Make it abstract!

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 5

7 / 41

Abstract classes

Solution:

Make it abstract!

public abstract class GeometricObject {

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 5

8 / 41

Abstract classes

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

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 5

9 / 41

Abstract classes

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

Then define “concrete” concepts as subclasses. (Circle, Rectangle, etc.)

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 5

10 / 41

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.

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 5

11 / 41

Abstract classes

Every GeometricObject has an area.

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 5

12 / 41

Abstract classes

Every GeometricObject has an area.

But getArea() defined differently for concrete objects.

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 5

13 / 41

Abstract classes

Every GeometricObject has an area.

But getArea() defined differently for concrete objects.

(Not defined for the GeometricObject class)

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 5

14 / 41

Abstract classes

// Circlepublic class Circle extends GeometricObject {

public float getArea() {return Math.PI * radius * radius;

}}

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 5

15 / 41

Abstract classes

// Circlepublic class Circle extends GeometricObject {

public float getArea() {return Math.PI * radius * radius;

}}

// Rectanglepublic class Rectangle extends GeometricObject {

public float getArea() {return width * height;

}}

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 5

16 / 41

Abstract classes

GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 5

17 / 41

Abstract classes

GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

Will not Compile! Cannot find “getArea()” in the GeometricObject class.

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 5

18 / 41

Abstract classes

So make getArea() an abstract method in GeometricObject.

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 5

19 / 41

Abstract classes

So make getArea() an abstract method in GeometricObject.

Only a declaration in GeometricObject:

public abstract class GeometricObject {...

public abstract float getArea();}

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 5

20 / 41

Abstract classes

GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

Will now work! getArea() defined in GeometricObject

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 5

21 / 41

Abstract class characteristics

Class has abstract methods = has to be an abstract class

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 5

22 / 41

Abstract class characteristics

Class has abstract methods = has to be an abstract class

But: Abstract class can have no abstract methods.

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 5

23 / 41

Abstract class characteristics

Class has abstract methods = has to be an abstract class

But: Abstract class can have no abstract methods.

Subclass can be abstract, superclass can be concrete.

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 5

24 / 41

Abstract class characteristics

Concrete subclasses must define superclass abstract methods. Circle, Rectangle have to define their own getArea();

Abstract methods can be defined, but usually aren’t.

Abstract classes cannot be instantiated.

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 5

25 / 41

Abstract class characteristics

Abstract class can be used as a data type though.

Example:

GeometricObject gObj;

GeometricObject[] objs = new GeometricObject[2];

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 5

26 / 41

Abstract class characteristics

Why is this allowed:

GeometricObject[] objs = new GeometricObject[2];

They can’t be instantiated, so why allow as data types?

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 5

27 / 41

Abstract class characteristics

GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

Can take advantage of polymorphism!

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 5

28 / 41

Abstract classes

Let’s say we need to check if areas of GeometricObjects are equal

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 5

29 / 41

Abstract classes

Let’s say we need to check if areas of GeometricObjects are equal

Define method called equalArea(...)

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 5

30 / 41

Abstract classes

public static void main(String[] args) {Circle c1 = new Circle(3.0);Rectangle r1 = new Rectangle(2, 3);Rectangle r2 = new Rectangle(1, 6);

SOP(“c1 and r1 have equal area: “ + equalArea(c, r1));

SOP(“r2 and r1 have equal area: “ + equalArea(r2, r1));

}

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 5

31 / 41

Abstract classes

How do you define equalArea(...)?

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 5

32 / 41

Abstract classes

How do you define equalArea(...)?

public static boolean equalArea( ? obj1, ? obj2) {return obj1.getArea() == obj2.getArea();

}

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 5

33 / 41

Abstract classes

Could define it for all different comparisons:

public static boolean equalArea( Circle c1, Rectangle r1) {

return c1.getArea() == r2.getArea(); }

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 5

34 / 41

Abstract classes

Could define it for all different comparisons:

public static boolean equalArea( Circle c1, Circle c2) {

return c1.getArea() == c2.getArea(); }

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 5

35 / 41

Abstract classes

Could define it for all different comparisons:

public static boolean equalArea( Rectangle r1, Rectangle r2) {

return r1.getArea() == r2.getArea(); }

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 5

36 / 41

Abstract classes

Could define it for all different comparisons:

public static boolean equalArea( Rectangle r1, Rectangle r2) {

return r1.getArea() == r2.getArea(); }

Tedious...

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 5

37 / 41

Abstract classes

Make it general thanks to abstract methods/classes:

public static boolean equalArea( GeometricObject g1, GeometricObject g2) {

return g1.getArea() == g2.getArea(); }

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 5

38 / 41

Abstract classes

Why not use the Object class?

public static boolean equalArea( Object g1, Object g2) {

return g1.getArea() == g2.getArea(); }

What’s the problem?

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 5

39 / 41

Abstract classes

Why not use the Object class?

public static boolean equalArea( Object g1, Object g2) {

return g1.getArea() == g2.getArea(); }

What’s the problem?Object class has no getArea() method!

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 5

40 / 41

Abstract classes

Why not use the Object class?

public static boolean equalArea( Object g1, Object g2) {

return g1.getArea() == g2.getArea(); }

What’s the problem?Object class has no getArea() method!

How would you fix it?

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 5

41 / 41

Abstract classes

Why not use the Object class?

public static boolean equalArea( Object g1, Object g2) {

GeometricObject go1 = (GeometricObject)g1;GeometricObject go2 = (GeometricObject)g2;return go1.getArea() == go2.getArea();

}

Cast the objects

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 5

42 / 41

Next Lecture...

Interfaces.