Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP 110 Inheritance Tabitha Peck M.S. April 14, 2008 MWF 3-3:50 pm Philips 367.

Similar presentations


Presentation on theme: "1 COMP 110 Inheritance Tabitha Peck M.S. April 14, 2008 MWF 3-3:50 pm Philips 367."— Presentation transcript:

1 1 COMP 110 Inheritance Tabitha Peck M.S. April 14, 2008 MWF 3-3:50 pm Philips 367

2 2 Announcements

3 3 Questions?

4 4 Today in COMP 110 Inheritance

5 5 Define a general class Later… define specialized classes Specialized class inherits properties from general class –Instance variables –Methods

6 6 Shapes Shape Rectangle Shape Rectangle Square

7 7 Base Class Shape Color xLocation yLocation An existing class that a derived class is built upon

8 8 Derived Class Shape Color xLocation yLocation Derived class inherits variables and methods from base class Rectangle length width

9 9 Shape Color xLocation yLocation setShapeColor translateShape Rectangle length width drawShape scaleShape Rectangle r = new Rectangle r.setShapeColor(Color.GREEN)

10 10 Derived Class Rectangle length width drawShape scaleShape calcArea calcPerim Square setSize scaleShape

11 11 extends public class Derived_Class_Name extends Base_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods } public class Rectangle extends Shape { }

12 12 Overriding Functions Overloading functions? Rectangle length width drawShape scaleShape calcArea calcPerim Square setSize scaleShape Shape Color xLocation yLocation area perimeter setShapeColor translateShape

13 13 super Call to base class

14 14 public class Shape { private Color color = new Color(0,0,0); private int xLocation = 0; private int yLocation =0; public void setShapeColor(Color c) { color = c; } public Color getShapeColor() { return color; } public void translateShape(int x, int y) { xLocation = x; yLocation = y; } public int[] getLocation() { int[] loc = {xLocation, yLocation}; return loc; } }

15 15 public class Rectangle extends Shape { private int length; private int width;

16 16 public void drawShape(Graphics g) { int[] loc = super.getLocation(); g.setColor(super.getShapeColor()); g.fillRect(loc[0], loc[1], length, width); }

17 17 public class Square extends Rectangle { public void setSize(int s) { super.setLength(s); super.setWidth(s); } public void scaleShape(int s) { super.scaleShape(s, s); } }

18 18

19 19 Wednesday More Inheritance Programming


Download ppt "1 COMP 110 Inheritance Tabitha Peck M.S. April 14, 2008 MWF 3-3:50 pm Philips 367."

Similar presentations


Ads by Google