29
Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 1030 1 / 15

EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

  • Upload
    others

  • View
    47

  • Download
    0

Embed Size (px)

Citation preview

Page 1: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Chapter 6: InheritanceEECS 1030

moodle.yorku.ca

moodle.yorku.ca EECS 1030 1 / 15

Page 2: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Questionnaire

Thanks for taking the time to fill it out.

moodle.yorku.ca EECS 1030 2 / 15

Page 3: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Questionnaire

Students ask questions in class (or online)

5 > 75% of the time8 50-75% of the time4 25-50% of the time2 < 25% of the time

Feel free to ask more questions

moodle.yorku.ca EECS 1030 3 / 15

Page 4: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Questionnaire

The pace of the lectures is appropriate

15 > 75% of the time1 50-75% of the time3 25-50% of the time1 < 25% of the time

moodle.yorku.ca EECS 1030 4 / 15

Page 5: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Questionnaire

The complexity of the material is appropriate

8 > 75% of the time8 50-75% of the time2 25-50% of the time2 < 25% of the time

moodle.yorku.ca EECS 1030 5 / 15

Page 6: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Questionnaire

“Lab submission system needs improving”

Yes, I fully agree. I have already improved it and I will keepworking on it.

One of the goals of the lab submission system is to get you familiarwith Linux, something that will also be very useful in futurecourses.

moodle.yorku.ca EECS 1030 6 / 15

Page 7: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

What do the classes ColouredRectangle and ColouredCircle have incommon?

Answer

The attribute colour, and the methods getColour, setColour,getArea and scale.

Question

How can we reduce the code duplication?

Answer

By introducing a super class that contains the common features.

moodle.yorku.ca EECS 1030 7 / 15

Page 8: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

What do the classes ColouredRectangle and ColouredCircle have incommon?

Answer

The attribute colour, and the methods getColour, setColour,getArea and scale.

Question

How can we reduce the code duplication?

Answer

By introducing a super class that contains the common features.

moodle.yorku.ca EECS 1030 7 / 15

Page 9: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

What do the classes ColouredRectangle and ColouredCircle have incommon?

Answer

The attribute colour, and the methods getColour, setColour,getArea and scale.

Question

How can we reduce the code duplication?

Answer

By introducing a super class that contains the common features.

moodle.yorku.ca EECS 1030 7 / 15

Page 10: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

What do the classes ColouredRectangle and ColouredCircle have incommon?

Answer

The attribute colour, and the methods getColour, setColour,getArea and scale.

Question

How can we reduce the code duplication?

Answer

By introducing a super class that contains the common features.

moodle.yorku.ca EECS 1030 7 / 15

Page 11: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Which features can be moved to the super class?

Answer

The attribute colour, and the methods getColour and setColour.

Question

Why can’t the methods getArea and scale be moved to the superclass?

Answer

Because their bodies are not the same.

moodle.yorku.ca EECS 1030 8 / 15

Page 12: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Which features can be moved to the super class?

Answer

The attribute colour, and the methods getColour and setColour.

Question

Why can’t the methods getArea and scale be moved to the superclass?

Answer

Because their bodies are not the same.

moodle.yorku.ca EECS 1030 8 / 15

Page 13: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Which features can be moved to the super class?

Answer

The attribute colour, and the methods getColour and setColour.

Question

Why can’t the methods getArea and scale be moved to the superclass?

Answer

Because their bodies are not the same.

moodle.yorku.ca EECS 1030 8 / 15

Page 14: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Which features can be moved to the super class?

Answer

The attribute colour, and the methods getColour and setColour.

Question

Why can’t the methods getArea and scale be moved to the superclass?

Answer

Because their bodies are not the same.

moodle.yorku.ca EECS 1030 8 / 15

Page 15: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Should the client be able to create a ColouredShape object?

Answer

Probably not.

Question

How can we prevent the client from creating a ColouredShapeobject?

Answer

Declare the class to be abstract.

moodle.yorku.ca EECS 1030 9 / 15

Page 16: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Should the client be able to create a ColouredShape object?

Answer

Probably not.

Question

How can we prevent the client from creating a ColouredShapeobject?

Answer

Declare the class to be abstract.

moodle.yorku.ca EECS 1030 9 / 15

Page 17: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Should the client be able to create a ColouredShape object?

Answer

Probably not.

Question

How can we prevent the client from creating a ColouredShapeobject?

Answer

Declare the class to be abstract.

moodle.yorku.ca EECS 1030 9 / 15

Page 18: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Should the client be able to create a ColouredShape object?

Answer

Probably not.

Question

How can we prevent the client from creating a ColouredShapeobject?

Answer

Declare the class to be abstract.

moodle.yorku.ca EECS 1030 9 / 15

Page 19: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Buying red paint

Problem

Given a list of ColouredShapes, we want to determine the totalarea of all red shapes combined, so that we know how much redpaint to buy.

moodle.yorku.ca EECS 1030 10 / 15

Page 20: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Abstract methods

Question

During early binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of the ColouredShape class.

Question

During late binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of a sub class of the ColouredRectangleclass.

moodle.yorku.ca EECS 1030 11 / 15

Page 21: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Abstract methods

Question

During early binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of the ColouredShape class.

Question

During late binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of a sub class of the ColouredRectangleclass.

moodle.yorku.ca EECS 1030 11 / 15

Page 22: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Abstract methods

Question

During early binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of the ColouredShape class.

Question

During late binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of a sub class of the ColouredRectangleclass.

moodle.yorku.ca EECS 1030 11 / 15

Page 23: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Abstract methods

Question

During early binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of the ColouredShape class.

Question

During late binding, to which class and method is the invocationshape.getArea() bound?

Answer

The getArea() method of a sub class of the ColouredRectangleclass.

moodle.yorku.ca EECS 1030 11 / 15

Page 24: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Why does each (non-abstract) sub class of ColouredShape have anon-abstract getArea() method?

Rule

Every non-abstract class has to implement all abstract methods ofits super class.

moodle.yorku.ca EECS 1030 12 / 15

Page 25: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Code duplication

Question

Why does each (non-abstract) sub class of ColouredShape have anon-abstract getArea() method?

Rule

Every non-abstract class has to implement all abstract methods ofits super class.

moodle.yorku.ca EECS 1030 12 / 15

Page 26: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Shape

Shape

Cube Ellipse

Circle

Rectangle

Square

Sphere

moodle.yorku.ca EECS 1030 13 / 15

Page 27: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Shape collection

Only Cube and Sphere have a volume.

Question

Can we introduce an abstract class HasVolume with methodgetVolume() as a superclass for Cube and Sphere?

Answer

No, because Cube and Sphere already have a superclass and Javadoes not support multiple inheritance.

moodle.yorku.ca EECS 1030 14 / 15

Page 28: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Shape collection

Only Cube and Sphere have a volume.

Question

Can we introduce an abstract class HasVolume with methodgetVolume() as a superclass for Cube and Sphere?

Answer

No, because Cube and Sphere already have a superclass and Javadoes not support multiple inheritance.

moodle.yorku.ca EECS 1030 14 / 15

Page 29: EECS 1030 moodle.yorku€¦ · Chapter 6: Inheritance EECS 1030 moodle.yorku.ca moodle.yorku.ca EECS 10301/15

Shape

<<interface>>HasVolume

Shape

Cube Ellipse

Circle

Rectangle

Square

Sphere

moodle.yorku.ca EECS 1030 15 / 15