Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Inner class inherit question

  Asked By: Karina    Date: Aug 04    Category: Java    Views: 1054
  

class A
{
int test() { return 1; }
class B
{
}
}

class C extends A.B
{
C(A a)
{
a.super();

System.out.println(test());
}
}

Most will probably first scratch their head on the the a.super()
thing. It looks kinda weird but it's correct.

The problem is with test(). I need to call the outer class' test
method from the derived C class. I can (ofcourse) call test from B,
but calling it from C gives a compiler error. I can't figure out how
to do this, if at all possible?

This also serves as proof to see if we have any advanced Java people here!

Share: 

 

19 Answers Found

 
Answer #1    Answered By: Jonathan Harrison     Answered On: Aug 04

Though my suggesition seems to be very simple, non-sense, and of course, u
all out there have thought of it, but as long as u have the instance of A that
you want to invoke its test() method, why don't u pass it as a prameter to the
constructor of B so that it would invoke it to the object with the refrence it
has.

And please all of you out there, keep all emails professional, the way we talk,
the way we ask question, the way we reply to question, the way we suggest a
solution. If some one thinks he's a guru, would you please ask question  polite,
and be specific in your words about the problem  you are solving, or about the
problem you are facing?
And if any one thinks that he's still a biggener, like myself, would you plz be
kind enough to thank the guru who solved your problem??

One last thing, we are all here to help each other in a way or another. Even
though I might be a begginer, I might ask a question that you, guru, didn't
think of before. And as for you gurus', you were a begginer and needed help
someday, didn't you??
And as for you begginers, you may be a guru someday and be asked questions like
the ones you are asking now, so ask them in the same way you prefer to be asked,
in a polite manner.

Thanks alot for all your time. And many many thanks to all of you for all the
help you are giving to each other.

 
Answer #2    Answered By: Frank Butler     Answered On: Aug 04

More a C++ person than a Java person, but ...

I don't think "a.super ()" is valid (or at least relevant). Parameter "a" is of
type "A" and class  "A" doesn't have a specified parent class.

I assume the parameter "a" will contain a reference to the associated "A" class
item. Why not then use "a.test()"?

> This also serves as proof to see if we have any advanced  Java people here!

This is very rude. I'm sure the capabilities of the people on this list range
between beginner and guru. Where do you sit on this range?

 
Answer #3    Answered By: Francis Riley     Answered On: Aug 04

Yeah well, people get frustrated. I don't mind chipping in helping
beginners, but with no feedback or any thank you's, there wasn't much
to go on.
I thought things might move a little quicker than on usenet.

Wrote in and wrote assemblers, compilers, interpreters, 3D stuff,
truck loads of software for over 20 years.

Back to the problem, the code really works, except for the test()
call. I don't think you tried to compile the code provided, try it.

The way I solve it now, is by adding a test() method to B and calling
A.this.test() from there.

I know it looks all pretty darn weird, but I wouldn't be asking if it
was straight forward or obvious.

 
Answer #4    Answered By: Alan Palmer     Answered On: Aug 04

You are correct, I didn't bother compiling it. After all, it doesn't actually
do anything. I merely commented on what you had posted. (And I prefaced my
reply by saying I was more a C++ person than a java  person.)

You didn't respond to my main point - i.e. the use of "a.test ()" which seemed
to me to be the best way to access A's test. Did this work?

I hold to my point about "a.super()" not being valid / relevant code. My
understanding is that this is a call  to the default constructor of the
parent/super class  of A, which is "java.lang.Object". While it is not wrong
syntax, I can't see what you hope to achieve by the call.

> I know it looks all pretty darn weird, but I wouldn't be asking if it
> was straight forward or obvious.

Unfortunately, nothing in your first message gave any indication that you had
any programming experience. Your use of meaningless class and method names and
your lack of any explanation of what requirement you were trying to address
certainly didn't give any impression that you were more than a beginner.

It looks weird  because it is unusual. Notice the lack of responses from any of
the Java gurus on this list. Post the actual requirement and you'll probably be
inundated with help on the "real" way to do it.

 
Answer #5    Answered By: Guadalupe Rogers     Answered On: Aug 04

To the original poster: there is a right way and a wrong way to ask a
question. David has given you everything you need to ask questions the
right way. And in case you need any additional guidance you can read
http://www.catb.org/~esr/faqs/smart-questions.html .

 
Answer #6    Answered By: Gustavo Taylor     Answered On: Aug 04

A little more complete version of the code in question:

class A
{
int test() { return  1; }
class B
{
int test() { return A.this.test(); } // this is the proxy I want
to do without
}
}

class C extends A.B
{
C(A a)
{
a.super();
}

void stuff()
{
System.out.println(test());
}

public static void main(String[] args)
{
A a = new A();
C c = new C(a);
c.stuff();
}
}

Ok, my appologies for the earlier confusing version.

> You are correct, I didn't bother compiling it. After all, it
doesn't actually do anything.

In order to concentrate on the issue at hand, I reduced a larger
scenario to a minimal set of classes.

> You didn't respond to my main point - i.e. the use of "a.test ()"
which seemed to me to be the best way to access A's test. Did this work?

In my previous sample I could have called a.test(), as you pointed
out. This is not what I ment however. I changed the sample code so
that I try to call  from a method where 'a' is not available, unless
manually stored.

> I hold to my point about "a.super()" not being valid / relevant code.

Try to comment out the a.super() call in C, you'll get an error:
'an enclosing instance that contains A.B is required'
or from jikes:
'Semantic Error: An instance of "A.this" is not accessible here. In
general, an enclosing instance is accessible only in the body of an
instance method, constructor (after the explicit constructor
invocation, if any), initializer block, or in the initializer
expression of an instance variable.'

The issue I wanted to get at, is that the 'test' proxy method in 'B'
is needed to make a 'test' call possible from 'C'.

Commenting out the 'test' proxy call in 'B' results in another
compiler error  (jikes):
'Semantic Error: No method named "test" was found in type "C".'

> It looks weird  because it is unusual. Notice the lack of responses
from any of the java  gurus on this list. Post the actual requirement
and you'll probably be inundated with help on the "real" way to do it.

I'll try to be more complete and clear next time.

It's not a useless scenario though. One could have a base service of
sorts that's represented in an inner class, enclosed by a managing
outer class. Then there could be a set of specialized classes that
derive from the inner service class.

Anyway, now that hopefully, it is more clear now, do you or anyone
else know how to call outer class  A's 'test' method directly from
derived inner class C's 'stuff', without storing any reference to A
(a), or any proxy method like I had to do?

 
Answer #7    Answered By: Velma Adams     Answered On: Aug 04

in response to your query of calling  the method in the outer class  from the
inner class, here is the solution.
declare the method test  in the outer class as static and then see.it works.
code:

class A
{
static int test() { return  1; }
class B
{
int test() { return A.test(); } // this is the proxy I want
to do without
}
}

class C extends A.B
{
C(A a)
{
a.super();
}

void stuff()
{
System.out.println(test());
}

public static void main(String[] args)
{
A a = new A();
C c = new C(a);
c.stuff();
}
}


pls let me know if u have any other clarifications needed in ur program.

 
Answer #8    Answered By: Wilbur Hall     Answered On: Aug 04

However, declaring the method as static is almost certainly not what is
required. The original example is too simplistic, as it produces a result which
does not use anything from the class  instance itself and would work perfectly
well as a static. The real requirement presumably needs information from the
class instance and therefore cannot be static.

 
Answer #9    Answered By: Chione Massri     Answered On: Aug 04

It was not David that asked the question, it was me.

I can't make the test  static, as test must be part of A's instance.
The test method will need to access A's member fields and methods. I
know the sample just shows "return 1;" but in real life it would be
doing more stuff, like interact with A's other things.

 
Answer #10    Answered By: Kay Rodriguez     Answered On: Aug 04


This code does compile and run. I've added some more features to the three
classes so that you can actually determine which method is being called. I
personally didn't think that "class C extends A.B" would extend class  B, but
it does. I didn't realize that you could extend an inner class in this
manner. It does show that the a.super() call  is not needed and does not
have an effect.

-Fred E Golder

public class A
{
int value = 1;
A()
{
value *= 10;
System.out.println("in A constructor");
}
int test() { return  ++value; }
int show() { return value; }

class B
{
int test() { return value/5; }
}

public static void main(String[] args)
{
A aaa = new A(); //expect value = 10
System.out.println("value is "+aaa.show()); //expect value =10
System.out.println("Test returns"+aaa.test()); //expect value = 11
C ccc = new C(aaa);
}
}

class C extends A.B
{
C(A a)
{
a.super();//expect there is no effect & optimized out of byte code
System.out.println("in C constructor");
System.out.println("Test returns"+a.test()); //expect value = 12
System.out.println("Test returns"+test()); //expect value = 2
}
}

 
Answer #11    Answered By: Paul Brooks     Answered On: Aug 04

without creating reference of the base class  using super, u cannot call  a
method of the outer class from the derived  inner class.
the basic rule of inheritance is tht when we r extending a base class, then
derived class should contain this super  method to initiate the constructor
of the base class.

 
Answer #12    Answered By: Sheryl Morgan     Answered On: Aug 04

> without creating reference of the base class  using super, u cannot
call a
> method of the outer class from the derived  inner class.

I'd say, yes, you can indeed not call  a method of the outer class from
the derived inner class. The "creating reference of the base class
using super" makes no sense. I think more correct  is "creating a
reference to the base class". I don't think you'd be "using super" to
get a reference to the outer class. "A.this" gets the reference to
the outer class.

I'm only trying to be accurate here btw.

> the basic rule of inheritance is tht when we r extending a base
class, then
> derived class should contain this super  method to initiate the
constructor
> of the base class.

This makes no sense. You are saying that a derived class should
contain the super method to initiate the constructor of the base class.
It's not about the "initiating" or calling  of the constructor that is
an issue in the sample. The "a.super()" really is correct syntax, if
that's what you were wondering.
"contain the super method". I can't figure  out what you're trying to
say here. The derived class should contain the test  method? I really
want the test method to be in the base outer class.

The only thing that's coming out of this whole exercise is that
"You can not call a method of an outer class from a class that's
derived from the outer class' inner class".
That is, "unless you have a reference to the outer class available in
the derived inner class".
Or, "unless the outer class' base inner class can delegate the outer
class' method call", which is what I was doing in my sample version 2.

Aside from all this, sample version 2 looks pretty darn strange, but
is all correct syntax, and works. I just wish I didn't need that
proxy delegate.

 
Answer #13    Answered By: Brian Ross     Answered On: Aug 04

A little more complete version of the code in question:

class A
{
int test() { return  1; }
class B
{
int test() { return A.this.test(); } // this is the proxy I want
to do without
}
}

class C extends A.B
{
C(A a)
{
a.super();
}

void stuff()
{
System.out.println(test());
}

public static void main(String[] args)
{
A a = new A();
C c = new C(a);
c.stuff();
}
}

Ok, my appologies for the earlier confusing version.

> You are correct, I didn't bother compiling it. After all, it
doesn't actually do anything.

In order to concentrate on the issue at hand, I reduced a larger
scenario to a minimal set of classes.

> You didn't respond to my main point - i.e. the use of "a.test ()"
which seemed to me to be the best way to access A's test. Did this work?

In my previous sample I could have called a.test(), as you pointed
out. This is not what I ment however. I changed the sample code so
that I try to call  from a method where 'a' is not available, unless
manually stored.

> I hold to my point about "a.super()" not being valid / relevant code.

Try to comment out the a.super() call in C, you'll get an error:
'an enclosing instance that contains A.B is required'
or from jikes:
'Semantic Error: An instance of "A.this" is not accessible here. In
general, an enclosing instance is accessible only in the body of an
instance method, constructor (after the explicit constructor
invocation, if any), initializer block, or in the initializer
expression of an instance variable.'

The issue I wanted to get at, is that the 'test' proxy method in 'B'
is needed to make a 'test' call possible from 'C'.

Commenting out the 'test' proxy call in 'B' results in another
compiler error  (jikes):
'Semantic Error: No method named "test" was found in type "C".'

> It looks weird  because it is unusual. Notice the lack of responses
from any of the java  gurus on this list. Post the actual requirement
and you'll probably be inundated with help on the "real" way to do it.

I'll try to be more complete and clear next time.

It's not a useless scenario though. One could have a base service of
sorts that's represented in an inner class, enclosed by a managing
outer class. Then there could be a set of specialized classes that
derive from the inner service class.

Anyway, now that hopefully, it is more clear now, do you or anyone
else know how to call outer class  A's 'test' method directly from
derived inner class C's 'stuff', without storing any reference to A
(a), or any proxy method like I had to do?

 
Answer #14    Answered By: Heidi Larson     Answered On: Aug 04

I wish everyone would have to sign an agreement that he has read the
smart-questions.html
and pass a test  about it BEFORE he is allowed to subscribe to a mailing
list.

At least the IDE discussion seems to has come to an end.

Sometimes I think of a cool business idea: Sell homeworks. You need a
calculator?
Get it for 10$. You have to check if a given character is numeric? Special
offer 20$
(sorry, but this is really difficult).
Currently I am setting up the website and I am searching for some venture
capital.
Unfortunately the www.help-me-I-am-dumb-and-lazy.com is not available.

 
Answer #15    Answered By: Nagina Mian     Answered On: Aug 04

> I wish everyone would have to sign an agreement that he has read the
> smart-questions.html
> and pass a test  about it BEFORE he is allowed to subscribe to a mailing
> list.
>
> At least the IDE discussion seems to has come to an end.
>
> Sometimes I think of a cool business idea: Sell homeworks. You need a
> calculator?
> Get it for 10$. You have to check if a given character is numeric? Special
> offer 20$
> (sorry, but this is really difficult).
> Currently I am setting up the website and I am searching for some venture
> capital.
> Unfortunately the www.help-me-I-am-dumb-and-lazy.com is not available.
>
> WBR
>
> Andreas

I hate being mean to new people, but sometimes it feels like I am
reading statements like:

> hihi2u my snobby lecturer wants me to write a program that prints out
> HelloWorld to the console, OMG what kind of super  human does he think
> I am. So if you could please just give me the source code for it, and
> preferably can you come over to my house and show me how to download
> the SDK because the only thing I know how to download is mp3s from
> > kazaa.
>
> PS can you compile it too.
>
> KTHXBI

I know it seems realy hard at the time, but I hope I never asked
"questions" like that.

 
Answer #16    Answered By: Whitney Cruz     Answered On: Aug 04

There is one other guideline to participating in a group conversation ... read
all the e-mails on the subject before replying.

Certainly, we all forget this on occasions, but the e-mail to which you are
replying was posted ten days ago, and there was an interchange that went on over
a period of days. I have nine messages saved in my archive and that's only a
portion of the interchange.

I think you will find that your suggestion is very similar to one I posted on
January 12. The constructor of C already gets the instance of A, so could use
it to call  A's "test". B doesn't need a reference to A in order to use "test".

 
Answer #17    Answered By: Asir Hashmi     Answered On: Aug 04

I just wanted to say that sometimes Yahoo mail can deliver mail in realy
retarded mannor. Sometimes I have recived one message for 14 days and
then gotten a backlog of email though (from days before my previous last
bunch of messages).

I don't know if this is the case here, but many times I have answered a
mail only to find out 2 days later that someone answered my mail the day
before I did.

 
Answer #18    Answered By: Saxon Anderson     Answered On: Aug 04

Interesting. I never get this - perhaps a delay of a few hours, but never days.

The only time this would change for me is when I'm using a moderated group and
the moderator is unable to get to the messages for a couple of days.

Next time this happens to you, it'd be worth sending a "did you get the delay
too?" to the group involved. If they didn't, then there may be some problem
with your e-mail chain of delivery.

 
Answer #19    Answered By: Geraldine Perkins     Answered On: Aug 04

I wouldn't be half surprised if some of my problesm comes from this
account (qgl.org). This is the only account I have problems with
yahoogroups though.

 
Didn't find what you were looking for? Find more on Inner class inherit question Or get search suggestion and latest updates.




Tagged: