Inheritance getter and toString() - java -
i'm practicing inheritance in java , got stuck on getter method in subclass. here point class:
package oop.linepoint; public class point { private int x; private int y; public point(int x, int y){ this.x = x; this.y = y; } public string tostring() { return "point: (" + x + "," + y + ")"; } public int getx() { return x; } public void setx(int x) { this.x = x; } public int gety() { return y; } public void sety(int y) { this.y = y; } }
here linesub class:
package oop.linepoint; public class linesub extends point{ point end; public linesub(int beginx, int beginy, int endx, int endy){ super(beginx, beginy); this.end = new point(endx, endy); } public linesub(point begin, point end){ super(begin.getx(),begin.gety()); this.end = end; } @override public string tostring() { return "linesub [begin=" + "(" + super.getx() +"," + super.gety() +") " + "end=" + end + "]"; } public point getend() { return end; } public void setend(point end) { this.end = end; } public point getbegin(){ } public void setbegin(point begin){ setx(begin.getx()); sety(begin.gety()); } }
my problem:
1) tostring() method. i'm trying print 2 points(begin , end). can see end easy begin point inherited , idk should type. way i'm getting x , y of point working me seems lame way of doing that. sure there better way, please me that?
2) point getbegin() method. i've tried:
public point getbegin(){ return (point)this; }//not working(getting whole point object)
and
public point getbegin(){ return new point(getx(), gety()); }//very noob way
i have no other ideas, please lend me wisdom.
to start point
must cast point
. can call super.tostring
access tostring
of parent class.
@override public string tostring() { return "linesub [begin=" + super.tostring() + "end=" + end.tostring() + "]"; } public point getbegin() { return (point) this; }
the fact have cast indicator have hierarchy wrong. structure normal implemented using 2 points.
public class linesub { point begin; point end;
Comments
Post a Comment