| |
|
2.7 Tying it all together
UNDER CONSTRUCTION: THIS SECTION IS NOT YET COMPLETE
Example:
Circle and Square are geometric objects that are generally termed Shape. We can thus describe Circle and Square as subclasses of class Shape.
Circle.java:
public class Circle extends Shape{ void draw(){ System.out.println("drawing circle"); //overriding the draw in Shape }
void printRadius() { System.out.println("the circle's radius is ..."); } }
|
Square.java:
public class Square extends Shape{
void draw(){ System.out.println("drawing square"); } }
|
Shape.java:
public class Shape{
void draw() { System.out.println("drawing shape"); }
void printRadius() { } }
|
GeomTest.java:
public class GeomTest {
public static void main(String[] args){ Shape[] s = new Shape[3]; s[0] = new Circle(); s[1] = new Circle(); s[2] = new Square(); for(int i=0;i<3;i++) { s[i].draw(); s[i].printSomething(); } } }
|
|
|