Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.

Similar presentations


Presentation on theme: "Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the."— Presentation transcript:

1 Week 5 Extending classes in Java

2 Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the parent class and the new one u The new class inherits all of the attributes and methods of the parent class and adds some of its own u The new class may override some of the methods of the parent class u The new class is a specialisation of the parent class

3 A real example u Objects of the class bank account will have common attributes (e.g. a balance) and methods (e.g. withdraw and deposit). u A savings account will be similar but will additionally give interest on the balance u A savings account is, therefore, a specialism of a bank account. u It inherits the attributes and methods u It adds extra functionality (methods) to pay interest on the balance

4 An ObjectWorld example  JTOval has attributes height and width  It has methods setHeight, setWidth and setPosition u A circle could be seen as a specialism of an oval; it has all the properties of an oval but has the property that its height and width are the same

5 JTCircle  To define a new class JTCircle that is a specialism of JTOval, we can add a new method, setDiameter, which would set the width and height to be the same: import JTShapes.*; public class JTCircle extends JTOval{ public void setDiameter (int newDia){ setHeight(newDia); setWidth(newDia); } }

6 Using JTCircle u Try this in User.java JTCircle trialCircle = new JTCircle(); trialCircle.setDiameter(40); u What happens?

7 This…

8 It inherits from JTOval u Try this… JTCircle trialCircle = new JTCircle(); trialCircle.setDiameter(40); trialCircle.setPosition(10,100);  Although JTCircle does not have a method called setPosition in it’s definition, this works because the method, setPosition, is inherited from JTOval.

9 But if we only do this… JTCircle trialCircle = new JTCircle(); u I.e. we don’t explicitly set the diameter

10 We still get an oval: u Why?

11 Because…  When a JTOval is created the height and width are set to 20 and 40, by default. u How? u With a constructor.

12 Constructors u Constructors are special methods that are invoked when an object is instantiated, e.g. public JTCircle() { setHeight(40); setWidth(40); } u This constructor sets the height and the width to default values of 40

13 Constructor naming u Notice that the constructor has the same name as the class. import JTShapes.*; public class JTCircle extends JTOval{ public JTCircle() { setHeight(40); setWidth(40); } public void setDiameter (int newDia){ setHeight(newDia); setWidth(newDia); } }  Instantiating a JTCircle will now produce a circle 40 pixels in diameter


Download ppt "Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the."

Similar presentations


Ads by Google