asked 63.7k views
2 votes
Consider the following class definition.public class Rectangle{private double length;private double width;public Rectangle(double l, double w){length = l;width = w;}public void set(double l, double w){length = l;width = w;}public void print(){System.out.println(length + " " + width);}public double area(){return length * width;}public double perimeter(){return 2 length + 2 width;}}Which of the following statements correctly instantiate the Rectangle object myRectangle?(i) myRectangle Rectangle = new Rectangle(10, 12);(ii) class Rectangle myRectangle = new Rectangle(10, 12);(iii) Rectangle myRectangle = new Rectangle(10, 12);

1 Answer

6 votes

Answer:

The answer is "option (iii)".

Explanation:

  • Instantiation stands for a specific instance creation of model or abstraction. It is also known as object code. In the given question the correct code for instantiating a class in java is option iii because it is the correct format.
  • In this option, we create a class object that is "myRectangle" and call the parameterized constructor by passing value in parameter this process is known as instantiation.
  • In instantiation first, we write a class name that is "Rectangle" then create an object that is "myRectangle" and use a new keyword for instantiating an object.
  • In option (i) we use the object name first and in option (ii) we use class keyword that's why both options are incorrect.

answered
User Paul Ho
by
7.7k points

No related questions found