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 revisionPrevious revision
Next revision
Previous revision
java:inheritance [2021/03/09 07:55] gthanosjava:inheritance [2022/03/11 05:21] (current) gthanos
Line 10: Line 10:
  
 <code java BasicRectangle.java> <code java BasicRectangle.java>
-class BasicRectangle {+public class BasicRectangle {
          
   int width;   int width;
Line 20: Line 20:
   }   }
  
-  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;
   }   }
      
-  int area() { +  public String toString() { 
-    return width height;+    return "width: "+width+", height: "+height;
   }   }
- 
 } }
 </code> </code>
Line 76: Line 75:
  
 <code java Rectangle.java> <code java Rectangle.java>
-class Rectangle extends BasicRectangle{+public class Rectangle extends BasicRectangle {
          
   Point origin;   Point origin;
Line 90: Line 89:
   }   }
          
-  void setOrigin(Point newOrigin) {+  public void setOrigin(Point newOrigin) {
     origin = newOrigin;     origin = newOrigin;
   }   }
      
-  Point getOrigin() {+  public Point getOrigin() {
     return origin;     return origin;
 +  }
 +  
 +  int area() {
 +    return width * height;
   }   }
        
   // 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.1615276552.txt.gz · Last modified: 2021/03/09 07:55 by gthanos