Is method(super) legal?

F

Felix Chu

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

Andrew Thompson

Felix said:
Can anyone please help me with this? I have two classes "parent" and
"child". The source code is below:
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.
 
A

Adam

Felix Chu said:
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
 
C

Chris Smith

Felix said:
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
 
T

Thomas Fritsch

Felix said:
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
 
A

Andrew Thompson

Felix said:
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?
 
F

Felix Chu

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

Felix Chu

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

Tony Morris

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)
 
X

xarax

Tony Morris said:
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()
}
}
 
T

Tony Morris

xarax said:
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)
 
X

xarax

Tony Morris said:
/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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top