cop 3503 fall 2012 shayan javed lecture 4

71
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1

Upload: nanda

Post on 16-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 4. Programming Fundamentals using Java. Inheritance. Derive new classes ( subclass ) from existing ones ( superclass ). Only the Object class ( java.lang ) has no superclass Every class = subclass of Object . Inheritance. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 4

1 / 71

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 4

Programming Fundamentals using Java

1

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 4

2 / 71

Inheritance

Derive new classes (subclass) from existing ones (superclass).

Only the Object class (java.lang) has no superclass

Every class = subclass of Object.

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 4

3 / 71

Inheritance

Code reuse – methods and properties.

Use classes to model objects of the same type

Define general class Then define specialized classes.

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 4

4 / 71

Inheritance

Can only inherit from one class.

Can inherit from multiple classes in other languages (C++)

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 4

5 / 71

Example

Let’s say you create the following classes: Circle Rectangle Triangle etc.

Share certain properties, but also specialized properties

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 4

6 / 71

Example

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

// constructors...public GeometricObject(String color, String name) {

…}

// get/set methods, etc.}

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 4

7 / 71

Examplepublic class Circle extends GeometricObject {

private double radius; // specific property

public Circle(double radius, String color, String name) {this.radius = radius;this.color = color;this.name = name;

}

// get/set methods, etc.

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

}}

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 4

8 / 71

Example

An error in the constructor!public Circle(double radius, String color, String name) {

this.radius = radius;this.color = color;this.name = name;

}

Why can’t we use “this.color” and “this.name”?

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 4

9 / 71

Example

They are private properties.

NOT accessible by subclasses.

Should use setColor() instead.

public methods/properties inherited.

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 4

10 / 71

Examplepublic class Rectangle extends GeometricObject {

private double width, height; // specific properties

public Rectangle(double w, double h, String color, String name) {this.height = h;this.width = w;setColor(color);setName(name);

}

// get/set methods, etc.

public double getArea() {return width * height;

}}

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 4

11 / 71

The super keyword

Are constructors inherited?

Yes. super used for: calling a super class constructor calling a super class method

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 4

12 / 71

The super keyword

Constructors:

super() calls the default constructor

super(arguments) calls the constructors according to the arguments

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 4

13 / 71

The super keyword

Constructors:public Circle(double radius, String color, String

name) {super(color, name);this.radius = radius;

}

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 4

14 / 71

The super keyword

Constructors:public Circle(double radius, String color, String

name) {super(color, name);this.radius = radius;

}

public GeometricObject(String color, String name) {this.color = color;this.name = name;

}

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 4

15 / 71

The super keyword

What if we don’t add the super(..) constructor call?

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 4

16 / 71

The super keyword

public Circle(double radius, String color, String name) {

this.radius = radius;}

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 4

17 / 71

The super keyword

public Circle(double radius, String color, String name) {

this.radius = radius;}

Is the same as:

public Circle(double radius, String color, String name) {

super();this.radius = radius;

}

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 4

18 / 71

The super keyword

Called Constructor Chaining

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 4

19 / 71

The super keyword

Calling the super class methods

super.method(parameters)...

Optional

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 4

20 / 71

Using get/set methods to access properties of the superclass is cumbersome…

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 4

21 / 71

The protected keyword

Often want subclasses to directly access properties/methods

Use protected instead of private/public

Subclasses can now directly access

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 4

22 / 71

Private

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

// constructors...

// get/set methods, etc.

}

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 4

23 / 71

The protected keyword

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

// constructors...

// get/set methods, etc.

}

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 4

24 / 71

The protected keyword

public class Circle extends GeometricObject {private double radius; // specific property

public Circle(double radius, String color, String name) {

this.radius = radius;this.color = color;this.name = name;

}

}

Now works!

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 4

25 / 71

Polymorphism

Important aspect of OOP

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 4

26 / 71

Polymorphism

Important aspect of OOP

“capability of an action or method to do different things based on the object that it is acting upon. ”

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 4

27 / 71

Polymorphism

Important aspect of OOP

“capability of an action or method to do different things based on the object that it is acting upon. ”

“characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.”

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 4

28 / 71

Polymorphism

1. Overriding Methods

2. Overloaded methods

3. Dynamic (late) method binding

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 4

29 / 71

Overriding Methods

Sometimes need to overwrite methods written in the superclass

Re-define the method in the subclass.

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 4

30 / 71

Overriding Methods

The Object class and toString()

toString() = String output when you print the object

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 4

31 / 71

Overriding Methods

The Object class and toString()

toString() = String output when you print the object

Object class has default toString() method

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 4

32 / 71

The toString() method

public String toString() {String str;...return str;

}

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 4

33 / 71

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5);

System.out.println(circle1);

Output:

?

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 4

34 / 71

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5);

System.out.println(circle1);

Output:

Circle@19821f

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 4

35 / 71

The toString() method

So override the method

public String toString() {String str = “”;str += “Circle: ” + getName() + “,”;str += “Color: ” + getColor() + “,”;str += “Radius: ” + getRadius();return str;

}

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 4

36 / 71

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5);

System.out.println(circle1);

Output:

Circle: Circle1,Color: Red,Radius: 3.5

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 4

37 / 71

The equals() method

The Object class and equals()

equals (Object obj) = returns whether the object passed in and this one are the same

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 4

38 / 71

The equals() method

Default implementation:

public boolean equals(Object obj) {return (this == obj);

}

Compares references

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 4

39 / 71

The equals() method

Circle circle1 = new Circle(3.5);

Circle circle2 = new Circle(3.5);

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 4

40 / 71

The equals() method

Circle circle1 = new Circle(3.5);

Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // ?

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 4

41 / 71

The equals() method

Circle circle1 = new Circle(3.5);

Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns false!

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 4

42 / 71

The equals() method

Circle circle1 = new Circle(3.5);Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns false!

Circle circle3 = circle1;

circle1.equals(circle3); // ?

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 4

43 / 71

The equals() method

Circle circle1 = new Circle(3.5);Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns false!

Circle circle3 = circle1;

circle1.equals(circle3); // returns true!

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 4

44 / 71

The equals() method

Override it

public boolean equals(Object obj) {if (obj instanceof Circle)

return this.radius == ((Circle)obj).getRadius();else

return false;}

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 4

45 / 71

The instanceof operator

Test if object is of a specified type

if (object instanceof Class)

Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 4

46 / 71

The instanceof operator

Test if object is of a specified type

if (object instanceof Class) Example:

Circle circle1 = new Circle(3.4);if (circle1 instanceof Circle) {…

}

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 4

47 / 71

The instanceof operator

Testing with the SuperClass:

if (subclassObj instanceof SuperClass) {…

}

?

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 4

48 / 71

The instanceof operator

Testing with the SuperClass:

if (Circle instanceof GeometricObject) {…

}

?

Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 4

49 / 71

The instanceof operator

Testing with the SuperClass:

if (subclassObj instanceof SuperClass) {…

}

returns true!

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 4

50 / 71

The instanceof operator

Example:

Circle circle1 = new Circle(3.4);if (circle1 instanceof GeometricObject) {…

}

returns true!

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 4

51 / 71

The instanceof operator

What if we write:

if (SuperClassObj instanceof subclass) {…

}

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 4

52 / 71

The instanceof operator

if (SuperClassObj instanceof subclass) {…

}

returns true as long as the object is an instance of the subclass

Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 4

53 / 71

The instanceof operator

Example:

Object is a superclass of Circle

public boolean equals(Object obj) {if (obj instanceof Circle)return this.radius == ((Circle)obj).getRadius();elsereturn false;

}

Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 4

54 / 71

The instanceof operator

Be careful with null objects

String s = null;

s instanceof String = false

Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 4

55 / 71

Object casting

So we can determine if (SuperClassObj instanceof subclass)

Convert it to subclass by casting

Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 4

56 / 71

Object casting

So we can determine if (SuperClassObj instanceof subclass)

Convert it to subclass by casting

Can now access members/properties

Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 4

57 / 71

Object casting

Once again...

public boolean equals(Object obj) {if (obj instanceof Circle) {

return this.radius == ((Circle)obj).getRadius();}else

return false;}

Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 4

58 / 71

Object casting

Once again...

public boolean equals(Object obj) {if (obj instanceof Circle) {

Circle circle = (Circle)obj; // castingreturn this.radius == circle.getRadius();

}else

return false;}

Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 4

59 / 71

Overloading Methods

Looked at it before.

Can have multiple methods with the same name.

But different parameters, return type

Page 60: COP 3503 FALL 2012 Shayan Javed Lecture 4

60 / 71

Dynamic binding

Object type is determined at run-time

ability of a program to resolve references to subclass methods at runtime.

Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 4

61 / 71

Dynamic binding

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

public float getArea() {return -1;

}

}

Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 4

62 / 71

Dynamic binding

// Circlepublic class Circle {

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

}}

// Rectanglepublic class Rectangle {

public float getArea() {return width * height;

}}

Page 63: COP 3503 FALL 2012 Shayan Javed Lecture 4

63 / 71

Dynamic binding

GeometricObject[] objs = new GeometricObject[2];

Page 64: COP 3503 FALL 2012 Shayan Javed Lecture 4

64 / 71

Dynamic binding

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

Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 4

65 / 71

Dynamic binding

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 66: COP 3503 FALL 2012 Shayan Javed Lecture 4

66 / 71

Dynamic binding

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()); }

What’s the output?

Page 67: COP 3503 FALL 2012 Shayan Javed Lecture 4

67 / 71

Dynamic binding

Output:9.02.0

The subclass getArea() methods are called.

At run-time, Java dynamically determines the class.

Page 68: COP 3503 FALL 2012 Shayan Javed Lecture 4

68 / 71

final Classes

Sometimes classes shouldn’t be allowed to be extended.

The String class for example. Use the keyword “final”

final class String {...}

Page 69: COP 3503 FALL 2012 Shayan Javed Lecture 4

69 / 71

final Methods

Methods which can’t be overridden in the subclasses.

Page 70: COP 3503 FALL 2012 Shayan Javed Lecture 4

70 / 71

Summary...

Inheritance super protected

Polymorphism Overloading Overwriting Dynamic binding

instanceof Object casting final classes and methods

Page 71: COP 3503 FALL 2012 Shayan Javed Lecture 4

71 / 71

Next lecture...

Abstract Classes

Interfaces