Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36108 > unrolled thread

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

Started bycdalten@gmail.com
First post2016-12-13 07:42 -0800
Last post2016-12-14 19:13 +0100
Articles 7 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  I don't understand why the following doesn't call Rectangle cdalten@gmail.com - 2016-12-13 07:42 -0800
    Re: I don't understand why the following doesn't call Rectangle Eric Douglas <e.d.programmer@gmail.com> - 2016-12-13 07:52 -0800
      Re: I don't understand why the following doesn't call Rectangle cdalten@gmail.com - 2016-12-13 08:06 -0800
        Re: I don't understand why the following doesn't call Rectangle Eric Douglas <e.d.programmer@gmail.com> - 2016-12-13 08:20 -0800
    Re: I don't understand why the following doesn't call Rectangle Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-13 11:42 -0500
    Re: I don't understand why the following doesn't call Rectangle lewbloch@gmail.com - 2016-12-13 12:47 -0800
      Re: I don't understand why the following doesn't call Rectangle Manuel Collado <m.collado@domain.invalid> - 2016-12-14 19:13 +0100

#36108 — I don't understand why the following doesn't call Rectangle

Fromcdalten@gmail.com
Date2016-12-13 07:42 -0800
SubjectI don't understand why the following doesn't call Rectangle
Message-ID<45806188-ffb3-4173-aaf0-48c84655fa85@googlegroups.com>
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?

[toc] | [next] | [standalone]


#36109

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-12-13 07:52 -0800
Message-ID<f58fc886-d35f-4d90-8286-c6e9cb949115@googlegroups.com>
In reply to#36108
On Tuesday, December 13, 2016 at 10:43:07 AM UTC-5, cda...@gmail.com wrote:
> 
> The author calls super() to construct a ComparableRectangle. How is this possible? Wouldn't super () invoke the Rectangle constructor, and thus construct a Rectangle?

You're new to Java?  super() is normally called within a constructor to inherit anything from the constructor of a class you're extending.  Without calling the super you're just overriding the constructor of the class you're extending, so you inherit methods but not initialization.

[toc] | [prev] | [next] | [standalone]


#36110

Fromcdalten@gmail.com
Date2016-12-13 08:06 -0800
Message-ID<94441613-81a8-4d22-8230-09735e5a91ae@googlegroups.com>
In reply to#36109
I think I have some serious misconceptions on how super() works. Also, is constructing a ComparableRectangle the same thing as constructing a ComparableRectangle object?

[toc] | [prev] | [next] | [standalone]


#36111

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-12-13 08:20 -0800
Message-ID<cd2fbc55-7b24-4372-8e6a-bf9826a7e04a@googlegroups.com>
In reply to#36110
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());
     }
}

[toc] | [prev] | [next] | [standalone]


#36112

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-12-13 11:42 -0500
Message-ID<o2p8bq$7vd$1@dont-email.me>
In reply to#36108
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)

[toc] | [prev] | [next] | [standalone]


#36117

Fromlewbloch@gmail.com
Date2016-12-13 12:47 -0800
Message-ID<afa8adde-a56e-44b0-907e-832c7ab6d165@googlegroups.com>
In reply to#36108
On Tuesday, December 13, 2016 at 7:43:07 AM UTC-8, cda...@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 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 Tutorials.

At a minimum.

>   }
> 
>   @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? 

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

> 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.

Read the Tutorials.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#36120

FromManuel Collado <m.collado@domain.invalid>
Date2016-12-14 19:13 +0100
Message-ID<o2s21o$13kr$1@gioia.aioe.org>
In reply to#36117
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web