Question

Suppose we have a class Rectangle and a class Square. Which should inherit from the other?...

Suppose we have a class Rectangle and a class Square. Which should inherit from the other? Defend your answer by discussing the class invariants as well as (at least) one example method and its preconditions and postconditions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Square class can inherit from the rectangle class.

Example code:

public class Main
{
   public static void main(String[] args) {
   Rectangle rectangle = new Rectangle(4, 6);
System.out.println(rectangle);
Square square = new Square(4);
System.out.println(square);
   }
}

class Rectangle {

private final int width;
private final int height;

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public int getArea() {
return width * height;
}

public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}

@Override
public String toString() {
return "This " + this.getClass().getSimpleName() + " has an area of " + getArea();
}

}
// Square is sub class inherting from rectangle class
class Square extends Rectangle {

public Square(int width) {
super(width, width);
}

@Override
public String toString() {
return super.toString();
}

}

Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again.

Add a comment
Know the answer?
Add Answer to:
Suppose we have a class Rectangle and a class Square. Which should inherit from the other?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Develop the ‘Shape’ application such that: · ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the...

    Problem: Develop the ‘Shape’ application such that: · ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. · Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and ‘Circle’ inherits from ‘Ellipse’. ‘Triangle’ has no derived class. · For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Each method should only have an output statement such as “Rectangle – draw method” that will be displayed when the method is invoked. · Implement the default constructors...

  • In Java, write a class Rectangle. This Rectangle class should have only the following public methods...

    In Java, write a class Rectangle. This Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write a method overlap(Rectangle other). This method should return true if this...

  • Create a class PictureFrame that is derived from class Rectangle. PictureFrame should have an boolean field...

    Create a class PictureFrame that is derived from class Rectangle. PictureFrame should have an boolean field for isHung and a method named hang which sets isHung to true.

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • In Python a class can inherit from more than one class (Java does not allow this)....

    In Python a class can inherit from more than one class (Java does not allow this). The resulting class will have all the methods and attributes from the parent classes. Do the following: • Create a class called Person. In the class, define variables for storing date of birth, place of birth, and male/female attributes. In the class, define the constructor method, as well as methods for returning current values of the class attributes. • Create a class called Employee....

  • c++ 6. Define classes shape, and classes circle and square, which inherit from shape. At the bottom of the page is a sample program using these classes, followed by its output. (a) Class shape h...

    c++ 6. Define classes shape, and classes circle and square, which inherit from shape. At the bottom of the page is a sample program using these classes, followed by its output. (a) Class shape has abstract virtual constant functions area and border which do not take arguments and which return double. It has a virtual constant function where which has no arguments and returns its lo- cation. It has a private field of type location and a ctor which accepts...

  • An abstract class doesn't have a constructor (because you cannot make an object of the abstract...

    An abstract class doesn't have a constructor (because you cannot make an object of the abstract class). It should have at least one method, which then has to be overridden in all derived classes. Here is an example:   abstract void run();   class Honda4 extends Bike{   public static void main(String args[]){   obj.run();   } You are to create an abstract class called Shape, which has an abstract method called computeArea(). Derive a Circle class from Shape. (Circle is similar to your previous...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT