• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheriting an innerclass

 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please check out the fist block of code

and then the second one

As you can see,the first block of code,the constructor of class IneritInner must receive a ContainInner reference in order to invoke the constructor of class Inner.the reason for doing such is obvious,that is inner class constructor must attach to the enclosing class object.However,in the second block of code InheritInner constructor do not any reference to the class ContainInner.why?
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fanny pan:

and then the second one


It is a bit of nest of vipers isn't it? However, if you look closely, you will realise that in the first example InheritInner extends ContainInner.Inner whereas in the second example InheritContainInner extends ContainInner directly. Thus InheritContainInner will directly call the default superclass constructor for you. The first example doesn't actually inherit from ContainInner and needs you to pass in an instance and call its superclass constructor.
Regards,
Ken
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody help me? To be honest, I'm a college student coming from china and currently preparing myself for the SCJP exam, since my college does not provide any java-related course, and i am the only one studying java in my class, and the only material about java i got is Thinking In Java third edition written by Bruce Eckle. So when i get caught in some problems on java(sometimes i feel lost and anonymous), the one that i considered to be my saver is you. hope you to be my learning partner. thank you for your generous supports.
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these:
class InheritInner extends ContainInner.Inner{
public InheritInner(ContainInner ci){ci.super();}
}
equivalent to the follow:
class InheritInner extends ContainInner.Inner{
public InheritInner(ContainInner ci){super();ci.super();}
}
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we have a class C as follow:
class A{
class B{
class C{}
}
}
and make a class named Test to inherit A.B.C, and how can we difine the class Test constructor.THX!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a academic question?
As far as I know, you use inner classes if you don't want to inherit from them.
Using innerclasses this way will make your projects hard to understand (as you see).
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fanny pan:
if we have a class C as follow:
class A{
class B{
class C{}
}
}
and make a class named Test to inherit A.B.C, and how can we difine the class Test constructor.THX!

Where *is* this stuff coming from?
As you have probably found, you cannot say

You can say

As Stefan has said this is stepping into the realms of the ridiculous, the whole point of an inner class being that it has an 'intimate' relationship with its superclass. Trying to inherit from an inner class in this way is a bit like trying to sleep with your brother's wife!
Regards,
Ken
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fanny pan:
Are these:
class InheritInner extends ContainInner.Inner{
public InheritInner(ContainInner ci){ci.super();}
}
equivalent to the follow:
class InheritInner extends ContainInner.Inner{
public InheritInner(ContainInner ci){super();ci.super();}
}

Time to write some code. A parameterless constructor which you write yourself is essentially the same as a default constructor supplied by the compiler. The difference is that you can add some stuff to find out what is going on. What would happen if you wrote a constructor such as

Why not give it try and experiment?
Regards,
Ken
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic