aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

suresh mit
Greenhorn

Joined: May 23, 2008
Posts: 10
In the following code,



When the above prog is executed, it gives compiler error. What is wrong in that code?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi suresh,

welcome to the ranch!

Did you take a look at the compiler error message? It would have told you exactly where the error in your code is! The problem is the following line because your inner class doesn't have a method sayHai().


Marco
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

It helps if you post the error. In this case, it's telling you that on line 10, it cannot find the symbol "sayHai()" in the class MyOuterClass.MyInnerClass...

Remember, an inner class does not automatically extend the enclosing class, so it does not inherit from the enclosing class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
suresh mit
Greenhorn

Joined: May 23, 2008
Posts: 10
Thanks for the reply.

When i access the private or public instance method of the outer class from the inner class method , it works perfectly. But i could not access that instance method using inner class object which is in the outer class.

The following code compiles and run without any errors.



When i uncomment that bold line, it gives error. Explain me why i could not access it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The method sayHai is in MyOuterClass. But "moi" does not reference an instance of MyOuterClass. Instead, "moi" references an instance of MyOuterClass.MyInnerClass, and this type of object does not have a method called sayHai(). Therefore, you cannot use the reference "moi" to call this method.

On the other hand, MyInnerClass is nested inside MyOuterClass. And in the context of MyOuterClass, the method sayHai() can be called directly -- even from within MyInnerClass.
suresh mit
Greenhorn

Joined: May 23, 2008
Posts: 10
I access the sayHai() method of MyOuterClass from sayHello() which is declared in MyInnerClass.It works correctly. So it means that the inner class has(has access to) that method. But Why i am not able to access that sayHai() public method using instance of inner class object?
Nareshreddy Kola
Greenhorn

Joined: May 24, 2008
Posts: 6
As per my knowledge *inner class can use outer class methods but through inner class you can't access outer class methods* This doen't mean inner class has access to outer class methods.
[ May 25, 2008: Message edited by: Nareshreddy Kola ]

Kola.Nareshreddy
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Can some one explain clearly on this.
Nareshreddy Kola
Greenhorn

Joined: May 24, 2008
Posts: 6
Method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type. I hope this should answer your question.
suresh mit
Greenhorn

Joined: May 23, 2008
Posts: 10
Originally posted by Nareshreddy Kola:
Method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type. I hope this should answer your question.


It means that, The inner class method can access and use the the outer class method but it does not has that method like in the inheritance. So we can not access the outer class method using inner class object reference. Am i right ?
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Iam not abel to get guys, what are you trying to sy, can anyone please explain in little simple terms. I am totally getting confused.


Thanks<br />Dinesh
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

In this example, MyInnerClass is an inner class of MyOuterClass.

It is not a subclass of MyOuterClass.

As stated in the Java Tutorial:
an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.


That is why the second code example, where MyInnerClass directly uses sayHai() from MyOuterClass is possible.

Because MyInnerClass is an inner class, not a subclass, of MyOuterClass, you cannot access sayHai() via an instance of MyInnerClass. MyInnerClass doesn't inherit the methods of MyOuterClass because it does not extend MyOuterClass.

An inner class and a subclass (class that extends) are different things.
[ May 27, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
suresh mit
Greenhorn

Joined: May 23, 2008
Posts: 10
Thanks Stevi Deter. Now I have understood the difference between inner class and subclass.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Always glad to know I've been able to help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes