Write a class Mysquare – In the constructor, print out the following message “I’
ID: 3769779 • Letter: W
Question
Write a class Mysquare – In the constructor, print out the following message “I’m a quadrilateral, I have four right angles and I have four equal sides.” Write three classes Quadrilateral and Rectangle inherited from Quadrilateral and Square inherited from Rectangle The constructors (without parameter) of the above three classes will print out the following messages Quadrilateral – I’m a quadrilateral Rectangle – I have four right angles Square – I have four equal sides Write a demo class to create objects of above four classes
Explanation / Answer
package current;
public class MySquare {
public MySquare () {
System.out.println("I’m a quadrilateral, I have four right angles and I have four equal sides");
}
public static void main(String[] theArgs) {
System.out.println("--------------------MySquare Constructor------------------------");
MySquare msq = new MySquare();
System.out.println("--------------------Quadrilateral Constructor------------------------");
Quadrilateral quad = new Quadrilateral();
System.out.println("--------------------Rectangle Constructor------------------------");
Rectangle rect = new Rectangle();
System.out.println("--------------------Square Constructor------------------------");
Square square = new Square();
}
}
class Square extends Rectangle {
public Square () {
System.out.println(" I have four equal sides");
}
}
class Rectangle extends Quadrilateral {
public Rectangle() {
System.out.println("I have four right angles");
}
}
class Quadrilateral {
public Quadrilateral() {
System.out.println("I’m a quadrilateral");
}
}
--------------------output----------------------------------
--------------------MySquare Constructor------------------------
I’m a quadrilateral, I have four right angles and I have four equal sides
--------------------Quadrilateral Constructor------------------------
I’m a quadrilateral
--------------------Rectangle Constructor------------------------
I’m a quadrilateral
I have four right angles
--------------------Square Constructor------------------------
I’m a quadrilateral
I have four right angles
I have four equal sides
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.