Created
June 3, 2019 11:15
-
-
Save dnivra26/73d64ac51167aad63e9c6ad568e4a2a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rectangle { | |
int width; | |
int height; | |
public Rectangle(int width, int height) { | |
this.width = width; | |
this.height = height; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public int getArea() { | |
return width * height; | |
} | |
public static void main(String[] args) { | |
Rectangle rectangle = new Rectangle(5, 6); | |
System.out.println(rectangle.getArea()); | |
Square square = new Square(4); | |
square.setHeight(5); | |
System.out.println(square.getArea()); | |
Rectangle re = new Square(4); | |
re.setWidth(4); | |
re.setHeight(5); | |
//assert this to be 20 | |
} | |
} | |
class Square extends Rectangle { | |
public Square(int side) { | |
super(side, side); | |
} | |
@Override | |
public void setWidth(int width) { | |
this.width = width; | |
this.height = width; | |
} | |
@Override | |
public void setHeight(int height) { | |
this.height = height; | |
this.width = height; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment