User Tools

Site Tools


java:inheritance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
java:inheritance [2021/03/05 17:26]
gthanos
java:inheritance [2021/04/02 14:19]
gthanos
Line 10: Line 10:
  
 <code java BasicRectangle.java> <code java BasicRectangle.java>
-class BasicRectangle {+public class BasicRectangle {
          
   int width;   int width;
   int height;   int height;
    
-  public Rectangle(int initWidth, int initHeight) {+  public BasicRectangle(int initWidth, int initHeight) {
     width = initWidth;     width = initWidth;
     height = initHeight;     height = initHeight;
   }   }
  
-  void setWidth(int newWidth ) {+  public void setWidth(int newWidth ) {
     width = newWidth;     width = newWidth;
   }   }
          
-  void setHeight(int newHeight ) {+  public void setHeight(int newHeight ) {
     height = newHeight;     height = newHeight;
   }   }
      
-  int getWidth() {+  public int getWidth() {
     return width;     return width;
   }   }
          
-  int getHeight() {+  public int getHeight() {
     return height;     return height;
   }   }
Line 39: Line 39:
     return width * height;     return width * height;
   }   }
 +   
 +  public String toString() { 
 +    return "width: "+width+", height: "+height; 
 +  }
 } }
 </code> </code>
Line 76: Line 79:
  
 <code java Rectangle.java> <code java Rectangle.java>
-class Rectangle extends BasicRectangle{+public class Rectangle extends BasicRectangle {
          
   Point origin;   Point origin;
Line 90: Line 93:
   }   }
          
-  void setOrigin(Point newOrigin) {+  public void setOrigin(Point newOrigin) {
     origin = newOrigin;     origin = newOrigin;
   }   }
      
-  Point getOrigin() {+  public Point getOrigin() {
     return origin;     return origin;
   }   }
        
   // Move rectangle origin by dx,dy   // Move rectangle origin by dx,dy
-  void moveOrigin(int dx, int dy) {+  public void moveOrigin(int dx, int dy) {
     origin.setX( origin.getX() + dx );     origin.setX( origin.getX() + dx );
     origin.setY( origin.getY() + dy );     origin.setY( origin.getY() + dy );
 +  }
 +  
 +  public String toString() {
 +    String str = origin.toString() + " ";
 +    str = str + super.toString();
 +    return str;
   }   }
 } }
java/inheritance.txt · Last modified: 2022/03/11 05:21 by gthanos