f



I don't understand why the following doesn't call Rectangle

In the following lines of code....

  
public class ComparableRectangle extends Rectangle 
    implements Comparable<ComparableRectangle> {
  /** Construct a ComparableRectangle with specified properties */
  public ComparableRectangle(double width, double height) {
    super(width, height);
  }

  @Override // Implement the compareTo method defined in Comparable 
  public int compareTo(ComparableRectangle o) {
    if (getArea() > o.getArea())
      return 1;
    else if (getArea() < o.getArea())
      return -1;
    else
      return 0;
  }
  
  @Override // Implement the toString method in GeometricObject
  public String toString() {
    return "Width: " + getWidth() + " Height: " + getHeight() +
      " Area: " + getArea();
  }
}

The author calls super() to construct a ComparableRectangle. How is this possible? Wouldn't super () invoke the Rectangle constructor, and thus construct a Rectangle?
0
cdalten
12/13/2016 3:42:48 PM
comp.lang.java.programmer 52714 articles. 0 followers. Post Follow

6 Replies
21 Views

Similar Articles

[PageSpeed] 52

On Tuesday, December 13, 2016 at 10:43:07 AM UTC-5, cda...@gmail.com wrote:
>=20
> The author calls super() to construct a ComparableRectangle. How is this =
possible? Wouldn't super () invoke the Rectangle constructor, and thus cons=
truct a Rectangle?

You're new to Java?  super() is normally called within a constructor to inh=
erit anything from the constructor of a class you're extending.  Without ca=
lling the super you're just overriding the constructor of the class you're =
extending, so you inherit methods but not initialization.
0
Eric
12/13/2016 3:52:54 PM
I think I have some serious misconceptions on how super() works. Also, is constructing a ComparableRectangle the same thing as constructing a ComparableRectangle object?
0
cdalten
12/13/2016 4:06:37 PM
On Tuesday, December 13, 2016 at 11:06:46 AM UTC-5, cda...@gmail.com wrote:
> I think I have some serious misconceptions on how super() works. Also, is constructing a ComparableRectangle the same thing as constructing a ComparableRectangle object?

The values in the constructor is what you're missing.  When you specify a value without calling super you're calling the super constructor without the value.  Try the following test with and without the super.


public class TestClass1 {
     private int value = 0;
     public TestClass1() {
          
     }
     public TestClass1(int setValue) {
          value = setValue;
     }
     public int getValue() {
          return value;
     }
}
public class TestClass2 extends TestClass1 {
     public TestClass2(int setValue) {
          super(setValue);
     }
     public static void main(String[] args) {
          TestClass2 testMe = new TestClass2(6);
          System.out.println(testMe.getValue());
     }
}
0
Eric
12/13/2016 4:20:56 PM
On 12/13/2016 10:42 AM, cdalten@gmail.com wrote:
> In the following lines of code....
>
>
> public class ComparableRectangle extends Rectangle
>     implements Comparable<ComparableRectangle> {
>   /** Construct a ComparableRectangle with specified properties */
>   public ComparableRectangle(double width, double height) {
>     super(width, height);
>   }
>[...]
>
> The author calls super() to construct a ComparableRectangle. How is this possible? Wouldn't super () invoke the Rectangle constructor, and thus construct a Rectangle?

     Yes!

     Keep in mind that a ComparableRectangle *is* a Rectangle -- a
specialized kind of Rectangle, but a Rectangle nonetheless.  Since
it is a Rectangle, it has a "Rectangle aspect" or "Rectangle-ness"
that needs to be initialized.  What piece of Java code initializes
the Rectangle-nature of things that are Rectangles?  The Rectangle
constructor, of course.

     Furthermore, a Rectangle *is* an Object, so the first thing
the Rectangle constructor does is invoke the Object constructor to
set up its Object-ness.  An Object *isn't* anything more general,
so the Object constructor needn't invoke anything further up the
line: It just initializes the Object-nature and returns to the
Rectangle constructor.  Which, in turn, initializes the Rectangle-
ness and returns to the ComparableRectangle constructor.  Which
has nothing more to do (in this case), so it just returns, having
arranged to set up the ComparableRectangle-ness, the Rectangle-ness,
and the Object-ness of the new instance.

     The ComparableRectangle constructor sets up a ComparableRectangle,
which is also a Rectangle, which is also an Object.  All three, at
the same time.

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/13/2016 4:42:52 PM
On Tuesday, December 13, 2016 at 7:43:07 AM UTC-8, cda...@gmail.com wrote:
> In the following lines of code....
>=20
>  =20
> public class ComparableRectangle extends Rectangle=20
>     implements Comparable<ComparableRectangle> {
>   /** Construct a ComparableRectangle with specified properties */
>   public ComparableRectangle(double width, double height) {
>     super(width, height);

The Java Tutorials cover this.
https://docs.oracle.com/javase/tutorial/java/IandI/super.html

It's a really, really good idea to be familiar with the material in the Tut=
orials.

At a minimum.

>   }
>=20
>   @Override // Implement the compareTo method defined in Comparable=20
>   public int compareTo(ComparableRectangle o) {
>     if (getArea() > o.getArea())
>       return 1;
>     else if (getArea() < o.getArea())
>       return -1;
>     else
>       return 0;
>   }
>  =20
>   @Override // Implement the toString method in GeometricObject
>   public String toString() {
>     return "Width: " + getWidth() + " Height: " + getHeight() +
>       " Area: " + getArea();
>   }
> }
>=20
> The author calls super() to construct a ComparableRectangle. How is this =
possible?=20

See the Tutorials. Every object (except an instance of `Object` directly) m=
ust initialize the parts of itself that it inherits. So *every* subtype of =
`Object` implicitly or explicitly calls an overload of `super()` as the fir=
st step in its own constructor.=20

> Wouldn't super () invoke the Rectangle constructor, and thus construct a =
Rectangle?

No, because it is part of the subtype constructor, so all it does is initia=
lize (which is what "construct" means) the parts of the object that are def=
ined in its supertypes.

Read the Tutorials.

--=20
Lew

0
lewbloch
12/13/2016 8:47:05 PM
El 13/12/2016 21:47, lewbloch@gmail.com escribi�:
> On Tuesday, December 13, 2016 at 7:43:07 AM UTC-8, cda...@gmail.com wrote:
> [...snipped...]
>> Wouldn't super () invoke the Rectangle constructor, and thus construct a Rectangle?
>
> No, because it is part of the subtype constructor, so all it does is initialize (which is what "construct" means) the parts of the object that are defined in its supertypes.

Yes. Probably the term "constructor" is misguiding. A constructor 
doesn't create an object, but just initialize its contents. It is the 
"new" operator which actually creates objects.

Well, this is how I see the picture.
-- 
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

0
Manuel
12/14/2016 6:13:33 PM
Reply: