Is method(super) legal?

  • Follow


Can anyone please help me with this? I have two classes "parent" and
"child". The source code is below:

class parent {
   void method1(parent p){};
}

class child extends parent {

   void child_method() {
     method1(super);
   }
}

is method1(super) legal? I try to compile but the error message:

child.java:24: '.' expected
     method1(super);
                  ^
child.java:24: <identifier> expected
     method1(super);

comes up.

Thanks in advance..



0
Reply felixchu (3) 2/3/2004 2:56:25 PM

Felix Chu wrote:
> Can anyone please help me with this? I have two classes "parent" and
> "child". The source code is below:
<snip>
> is method1(super) legal? I try to compile but the error message:

No.  super can only be called from
the constructor, and needs to be the
first statement.

What end result are you trying to
achieve?  There is probably a way
to do it.

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology


0
Reply SeeMySites (3836) 2/3/2004 2:56:56 PM


"Felix Chu" <felixchu@optushome.com.au> wrote in message
news:401fb302$0$4260$afc38c87@news.optusnet.com.au...
> Can anyone please help me with this? I have two classes "parent" and
> "child". The source code is below:
>
> class parent {
>    void method1(parent p){};
> }
>
> class child extends parent {
>
>    void child_method() {
>      method1(super);
>    }
> }
>
> is method1(super) legal?

No. You want
method1(this);
'this' is an instance of 'parent'. Exactly the one that you are trying to
pass.

To simplify, you might do this: Define
method1OnMyself(){ method1(this);    }
in parent class,
call it in the child class and voila - that's what you tried to achieve.

But it does not make much sense. A reference of the instance that you are
trying to pass
as a parameter is already accessible in method1 and it's called 'this'.
So in method method1(parent p) you would have this==p after what you do
here.

Adam


0
Reply notforspam.a_szczeblewski (4) 2/3/2004 3:01:38 PM

Felix Chu wrote:
> Can anyone please help me with this? I have two classes "parent" and
> "child". The source code is below:
> 
> class parent {
>    void method1(parent p){};
> }
> 
> class child extends parent {
> 
>    void child_method() {
>      method1(super);
>    }
> }
> 
> is method1(super) legal? I try to compile but the error message:

No, it's not legal... of course, the compiler already told you that.  If 
you explain what you want to accomplish, then someone can certainly 
suggest a way to reach that goal.  However, the code above is rather 
non-sensical, so I can't guess what you want.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
0
Reply cdsmith (3171) 2/3/2004 3:05:28 PM

Felix Chu wrote:

>Can anyone please help me with this? I have two classes "parent" and
>"child". The source code is below:
>
>class parent {
>   void method1(parent p){};
>}
>
>class child extends parent {
>
>   void child_method() {
>     method1(super);
>
I guess

       method1(this);

is, what you intended, or almost equivalently:

       super.method1(this);

>   }
>}
>
>is method1(super) legal? I try to compile but the error message:
>
>child.java:24: '.' expected
>     method1(super);
>                  ^
>child.java:24: <identifier> expected
>     method1(super);
>
>comes up.
>
>Thanks in advance..
>  
>
 
______________________________________________________
Thomas <dot> Fritsch <at> ops <dot> de

0
Reply i.dont.like (22) 2/3/2004 3:26:40 PM

Felix Chu wrote:
> Thanks for your help guys. Actually I extract the code from a real
> application (I tried to compile its source code but got this super
> keyword error).

(scratches head) How can it be a
'real application' if it does not compile?

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology


0
Reply SeeMySites (3836) 2/3/2004 3:43:09 PM

Thanks for your help guys. Actually I extract the code from a real
application (I tried to compile its source code but got this super keyword
error).

The child class has a clone method which returns a copy of that object. E.g:

In parent.class:

void copyparent(parent p) {

 this.a = p.a;
 this.b = p.b;

}

In child.class:

child clone() {

   child cloneobj = new child();
   cloneobj.copyparent(super);
   cloneobj.c = this.c;
   return cloneobj;
}

method copyparent(parent) copies all the variables inherited by the
objects..



"Thomas Fritsch" <i.dont.like@spam.com> wrote in message
news:1075821462.540367@igate1.ops.de...
> Felix Chu wrote:
>
> >Can anyone please help me with this? I have two classes "parent" and
> >"child". The source code is below:
> >
> >class parent {
> >   void method1(parent p){};
> >}
> >
> >class child extends parent {
> >
> >   void child_method() {
> >     method1(super);
> >
> I guess
>
>        method1(this);
>
> is, what you intended, or almost equivalently:
>
>        super.method1(this);
>
> >   }
> >}
> >
> >is method1(super) legal? I try to compile but the error message:
> >
> >child.java:24: '.' expected
> >     method1(super);
> >                  ^
> >child.java:24: <identifier> expected
> >     method1(super);
> >
> >comes up.
> >
> >Thanks in advance..
> >
> >
>
> ______________________________________________________
> Thomas <dot> Fritsch <at> ops <dot> de
>


0
Reply felixchu (3) 2/3/2004 3:50:27 PM

I dunno too..coz I got all the class files along with the source codes, and
the class files work fine.


"Andrew Thompson" <SeeMySites@www.invalid> wrote in message
news:hkPTb.41157$Wa.34063@news-server.bigpond.net.au...
> Felix Chu wrote:
> > Thanks for your help guys. Actually I extract the code from a real
> > application (I tried to compile its source code but got this super
> > keyword error).
>
> (scratches head) How can it be a
> 'real application' if it does not compile?
>
> --
> Andrew Thompson
> * http://www.PhySci.org/ Open-source software suite
> * http://www.PhySci.org/codes/ Web & IT Help
> * http://www.1point1C.org/ Science & Technology
>
>


0
Reply felixchu (3) 2/3/2004 4:06:56 PM

> No.  super can only be called from
> the constructor, and needs to be the
> first statement.

The superclass constructor can only be called from the subclass constructor,
and will be the first statement (implicitly or explicitly) within all
constructors with the following exceptions:
- does not apply to java.lang.Object
- does not apply if an explicit call to this(...) is made

A reference called 'super' can also be dereferenced (not necessarily within
the constructor) to call a superclass method:

class A
{
    void m()
    {

    }
}

class B extends A
{
    void m2()
    {
        super.m();
    }
}



-- 
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


0
Reply dibblego (423) 2/4/2004 12:54:00 AM

"Tony Morris" <dibblego@optusnet.com.au> wrote in message
news:bvpfra$7j2$1@news.btv.ibm.com...
> > No.  super can only be called from
> > the constructor, and needs to be the
> > first statement.
>
> The superclass constructor can only be called from the subclass constructor,
> and will be the first statement (implicitly or explicitly) within all
> constructors with the following exceptions:
> - does not apply to java.lang.Object
> - does not apply if an explicit call to this(...) is made
>
> A reference called 'super' can also be dereferenced (not necessarily within
> the constructor) to call a superclass method:
>
> class A
> {
>     void m()
>     {
>
>     }
> }
>
> class B extends A
> {
>     void m2()
>     {
>         super.m();
>     }
> }

This is most commonly used to call the superclass instance
method of the same name.

class A
{
    void m()
    {
    }
}

class B extends A
{
    void m()
    {
        super.m(); // call the parent class version of m()
    }
}



0
Reply xarax (448) 2/4/2004 1:38:26 AM

"xarax" <xarax@email.com> wrote in message
news:m2YTb.12206$uM2.8650@newsread1.news.pas.earthlink.net...
> "Tony Morris" <dibblego@optusnet.com.au> wrote in message
> news:bvpfra$7j2$1@news.btv.ibm.com...
> > > No.  super can only be called from
> > > the constructor, and needs to be the
> > > first statement.
> >
> > The superclass constructor can only be called from the subclass
constructor,
> > and will be the first statement (implicitly or explicitly) within all
> > constructors with the following exceptions:
> > - does not apply to java.lang.Object
> > - does not apply if an explicit call to this(...) is made
> >
> > A reference called 'super' can also be dereferenced (not necessarily
within
> > the constructor) to call a superclass method:
> >
> > class A
> > {
> >     void m()
> >     {
> >
> >     }
> > }
> >
> > class B extends A
> > {
> >     void m2()
> >     {
> >         super.m();
> >     }
> > }
>
> This is most commonly used to call the superclass instance
> method of the same name.
>
> class A
> {
>     void m()
>     {
>     }
> }
>
> class B extends A
> {
>     void m()
>     {
>         super.m(); // call the parent class version of m()
>     }
> }
>
>
>

Yes, you're right - that is the most common use, since overriding hides the
superclass implementation and subclasses often want to provide that
implementation.
I used two different method names to prevent the question, "must the method
names be the same ?"
:)


-- 
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


0
Reply dibblego (423) 2/4/2004 2:41:20 AM

"Tony Morris" <dibblego@optusnet.com.au> wrote in message
news:bvpm4h$n8e$1@news.btv.ibm.com...
>
/snip/
> Yes, you're right - that is the most common use, since overriding hides the
> superclass implementation and subclasses often want to provide that
> implementation.
> I used two different method names to prevent the question, "must the method
> names be the same ?"
> :)

Of course you're right about that, too. The usage of super.method()
will invoke method() of the super class regardless of whether
it has been overridden in the current class, and it is not
restricted to a method of the same name. The super class
method must be accessible to the current class (not private or
package private by another package that is different from the
current class package).



0
Reply xarax (448) 2/4/2004 3:16:30 AM

11 Replies
30 Views

(page loaded in 0.645 seconds)

Similiar Articles:









7/12/2012 3:26:19 PM


Reply: