Lab5.2 Shape Inheritance Student Guide

User Manual: Pdf

Open the PDF directly: View PDF PDF.
Page Count: 6

Scroll down to view the document on your mobile browser.
 1  Lab5 part II:  Inheritance and the Shape Hierarchy*  CSSSKL162: Programming Methodology Skills Summary Build two classes that inherit from an existing Shape superclass.  These subclasses should all have the phrase “extends Shape” in their class signatures, as in: “public class MyNewShape extends Shape {“.  Your classes can and should be tested in isolation before combining your classes with the final drivers, so consider building a main in each subclass that tests out just that class.  When you are confident in your subclasses, you can use this driver to see your shapes render to a window (a JPanel).    Introduction This lab continues exploring the idea of inheritance, which is a critical concept in object-oriented programming.  Inheritance (as defined in the software realm) borrows from its Mendelian genetics: just as you can inherit your mother’s eyes or your father’s sense of humor, one child class can inherit characteristics and behaviors of a parent class.  More specifically to Java classes, when one child class inherits from a parent class (or a subclass inherits from a superclass), this child class gets a copy of all the methods and data from the parent class – you can envision this as a copy-and-paste operation, from the parent to the child.  Once the child class extends the parent class, any methods or data items found in the parent class are now a part of the child class, and the child class is free to define extra features.  In Venn diagram terms, the parent class is strictly a subset of the child class, and would look like:            What this picture highlights is the relationship between parent classes and child classes – everything the parent class has, the child also has.  More to the point: any public method defined in the parent will also be defined in the child class, and this acts as a type of contract or guarantee.  Indeed, it is a class invariant that the interface for a child class will contain every (public) method defined in the parent class interface (except for constructors).  If we can promise that a Circle class has every method found in the Shape class, then anywhere in software a Shape is called for, a Circle could be substituted (this concept is called substitutability and is related to polymorphism, but more on that later).  If we have a function that expects a Ball object, and we have a subclass object VolleyBall, then we can pass the VolleyBall to the method with no problems, because a VolleyBall is a Ball, just like a Circle is a Shape.  Inheritance then defines this type of “is a” relationship, which is a one-way relation between two classes.  Note that the relationship isn’t like the bi-conditional operator in that, all VolleyBalls are definitely Balls, but not all Child Class (Circle) Parent Class (Shape)
 2  Balls are guaranteed to be VolleyBalls (some could be BaseBalls or BasketBalls, for example).  When describing inheritance relationships, we’ll use an arrow to indicate this “one-way” characteristic.  When building classes that are interrelated via inheritance, inheritance hierarchies naturally arise; these are simply tree structures that display the “is a” inheritance relationships between the classes in your software.  Before we talk about the methods your Class must provide (or more specifically, override), we should get to know our Parent class Shape.  The methods and data for this class are outlined below, and the code is available for download via the website (and you’ll need it in the same directory as your child classes).   Data Members  int x;    //all shapes have an x,y coordinate pair in Java2D o Should this be public? Private?  int y;    //and so we’ll have the Parent class manage this data o What access modifier should we use here?  (optional) Color myColor; o Colors are immutable, and so will have no getters/setters and also will not suffer from privacy leaks. o Check with your lab instructor as to whether you will implement this.  Method Members  Shape(int x, int y) //a constructor used to initialize the data members  double getArea();  //used to calculate the area of this shape o For a Circle, use Math.PI * r * r, and for a Square, side * side.  void paint(Graphics g); //the paint method is called on each shape to draw itself o See the Spray subclass for a sample implementation of the paint function  This was draw() in previous Shape classes.  getX()     //accessor  getY()    //accessor  setX(int)  //mutator  setY(int)  //mutator    As you can see from above, the class is quite small.  Now take a look in the code to see how many of the Shape functions are actually empty!  Shape is really too generic of a class to offer an implementation for

Navigation menu