aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheritance in inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance in inner classes" Watch "inheritance in inner classes" New topic
Author

inheritance in inner classes

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

hello everyone
back to ranch after so many days
feeling good
I was playing with inner classes and found a little interesting things those are much similar to our normal classes
I am posting the code below
I don't have any question and I just want to share these interesting things
take a look at it
you will definitely like it
Happy coding


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Prasad Kharkar wrote:
I was playing with inner classes and found a little interesting things those are much similar to our normal classes
I am posting the code below
I don't have any question and I just want to share these interesting things
take a look at it
you will definitely like it


If you find this interesting... See if you can figure out how an inner class class can extend another inner class from a different outer class. Also, try to create some constructors that takes parameters too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

dear Henry sir
thank you for your suggestion
your post made me think a little deeper
here I tried to do something more interesting
I have created an interface and then implemented that in another class
thank your for your suggestion
have a nice time
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

sir
also I have tried to write constructor that take arguments
but they are not accepted at all
another difference I found is that when using interface, we do not have to extend the class that contains the interface
but in case of the class we have to extend the class that contains the inner class
why is that so?
please explain
here is my code
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Fixed all the compile errors in your program. In some cases, I just made changes to fix the compile error -- you'll need to fix the program itself, to do what you intended.



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40


BTW, it is much more interesting getting TestInnerClass to extend ClassInClass.InnerClass when TestClassInClass does *not* extend ClassInClass ...

Henry
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

while creating instance using the overloaded constructors why did that give errors?
I am unable to understand
how to create the instance of that class?
I really don't know
I mean I have tried definitely you know it also
but how to extend the inner class without extending the outer class ?
and why does this not apply to the interface?
please answer.
I am really eager to know
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

Prasad Kharkar wrote:while creating instance using the overloaded constructors why did that give errors?
I am unable to understand


Dear Prasad, is this related to Inner class concept ?
and if you have chance please post your works of constructor overloding along with errors , so that i can learn more.
by the way , i like your programs.

Cheers
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Hello ravi
I feel glad that you like my programs
I will definitely do some more work on inner classes as my end semester exams are over now and I have plenty of time for preparation
if you find such interesting things, let me know
we can discuss them online
I am sending you my email ID in Personal Message
will you be my friend?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Prasad Kharkar wrote:while creating instance using the overloaded constructors why did that give errors?
I am unable to understand


You had two errors. First, you added an argument to the InnerClass class (super class) constructor, but your TestInnerClass class (sub class) constructor didn't use it. Instead, it tried to use the no-arg constructor that didn't exist. Second, you didn't instantiate the TestInnerClass correctly -- don't know why you didn't, since you did instantiated the InnerClass correctly, and it is basically done the same way.

Prasad Kharkar wrote:
but how to extend the inner class without extending the outer class ?
and why does this not apply to the interface?
please answer.
I am really eager to know


I am not sure what you mean by what applying to the interface. Can you elaborate what you mean? Your interface example looks straightforward to me.

BTW, there is also a big difference between the static inner class (more commonly know as the nested class) and the inner class. The first is much easier to understand, as it is very similar to an outclass that is merely defined in the another class. The later is much more complex -- since it needs that outer class, and can get really complex when it needs multiple outer classes (as you are trying to figure out).

Henry
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

hahahaha
I have to agree that I am a fool
thank you Henry sir for pointing that out
sometimes I go blind
but I really don't know how to use the InnerClass for subclassing in another class without extending the outer class
also why does that work when we implement interface from another class
why does that not require the another class to subclass the class that has that interface?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Prasad Kharkar wrote:why does that work when we implement interface from another class


because interface dont have constructor . I ask you one thing, how do you instantiate the inner class[abstract static] which is inside an interface ?

<edit>also if the inner class[InnerClass] is static , then no need to extends the outer class[ClassInClass]</edit>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Prasad Kharkar wrote:
but I really don't know how to use the InnerClass for subclassing in another class without extending the outer class


Here you go... The TestClassInClass modified to not extend ClassInClass, which causes some changes in the inner classes which still does inherit from each other.



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Prasad Kharkar wrote:
also why does that work when we implement interface from another class
why does that not require the another class to subclass the class that has that interface?


I don't know why you are trying to compare the two. Interfaces and classes are not the same thing.

As from my previous post, even with inner classes, it doesn't require that the outer class be subclassed from each other. But the reason in your example, it was somewhat needed because inner classes contain a reference to its outer class, and subclassing the outer classes was one way to do it. My example was another way to do it.

As for interfaces, or even nested classes, this requirement (ref to outer class) does not exist.

Henry
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Thank you Henry sir
got it to much extent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance in inner classes