Saturday, 15 January 2011

java - Does "extends" also extend variables? -



java - Does "extends" also extend variables? -

i figured when extend object object, object receives of variables , definition of variables objects methods. when have code

import javax.swing.*; import java.awt.*; public abstract class object extends jcomponent{ private int xpos; private int ypos; private int height; private int width; private double rotate; private rectangle rect; public object(int p1, int p2, int w, int h, double r){ this.xpos = p1; this.ypos = p2; this.width = w; this.height = h; this.rotate = r; rect = new rectangle(xpos, ypos, width, height); } }

as original object, , object this:

import javax.swing.*; import java.awt.*; public class block extends object{ public block(int p1, int p2, int p3, int p4, double r) { super(p1, p2, p3, p4, r); } public void paintcomponent(graphics g){ graphics2d gd = (graphics2d) g; gd.setcolor(color.blue); gd.draw(rect); } }

why isn't rectangle "rect" beingness extended over? sorta new @ java , still learning please go easy on me xd

rect marked private in object. visible instances of object class. kid classes cannot see in implementation. can 2 things:

1) create getter method in object. method public, other class, including block class, can see it.

public rectangle getmyrect() { homecoming rect; }

2) or mark protected or default (no access modifier).

protected rectangle rect; // way... rectangle rect; // ... or way

p.s. not name classes object. work if class defined in different package, class object has special meaning -- top-most parent class of classes -- , confuse people.

java drawing hierarchy extends

No comments:

Post a Comment