java - Advice with super classes and inheritance -
i not know if on right track or not, have hit dead end. have read , reread books chapters super classes , inheritance , still lost. in assignment have to: write super class encapsulating circle; class has 1 attribute representing radius of circle. has methods returning perimeter , area of circle. class has subclass encapsulating cylinder. cylinder has circle base , attribute, length; has 2 methods, calculating , returning area , volumes.
i wondering if 1 make sure on right track , give me advice on need finish lost. thank you
first class:
package question48; import java.awt.graphics; import java.awt.color; public class question48 { public static void main(string[] args) { } public abstract class figure { //varibles private int x; private int y; private color color; //constructor public figure() { x = 0; y = 0; color = color.black; } public figure(int startx, int starty, color startcolor) { x = startx; y = starty; color = startcolor; } //accessor public color getcolor() { color tempcolor = color; return tempcolor; } //mutator public void setcolor(color newcolor) { color = newcolor; } public int getx() { return x; } public void setx(int newx) { x = newx; } public int gety() { return y; } public void sety(int newy) { x = newy; } public abstract void draw(graphics g); } }
second class:
package question48; import java.awt.graphics; import java.awt.color; public class circle extends question48 { private int radius; public circle() { super(); radius = 0; } public circle(int startx, int starty, color startcolor, int startradius) { super(startx, starty, startcolor); setradius(startradius); } public void setradius(int newradius) { radius = newradius; } public int getradius() { return radius; } public void draw(graphics g) { g.setcolor(getcolor()); g.filloval(getx(), gety(),radius*2, radius*2); } }
i can see 2 problems here.
first, made error declaring circle subclass of question48. subclassing (extends
keyword) way of telling derived class kind of base class (we call 'is a' relationship). should clear circle kind of figure (i.e. circle is a figure), , not kind of question48.
second, class figure
nested inside question48
class. designed way on purpose? simplier design create separate new java file figure
abstract class , separate files classes that'll extend (all classes considered 'top-level' then).
if you're not sure why recommend reading more differences between top-level, inner , nested classes in java. it's quite complex topic.
in short:
in situation subclassing figure
circle
requires circle
sibling nested class inside question48
class. this:
public class question48 { public static void main(string[] args) { } public abstract class figure { //... } public class circle extends figure { @override public void draw(graphics g) { //... } } // , here cylinder class // ... }
now, if you'll give design , extract classes separete java files clearer:
file question48.java:
public class question48 { public static void main(string[] args) { }
file figure.java:
public abstract class figure { // member fields , methods common figures }
file circle.java:
public class circle extends figure { // member fields , methods common circles , it's derivatives }
file cylinder.java:
public class cylinder extends circle { // member fields , methods common cylinders private circle base; private int length; //... // overriden circle's methods no longer valid cylinder's instances }
it's not clear me why cylinder must subclass of circle, that's assignemnt says won't discuss it.
Comments
Post a Comment