• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Classes

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code,



When the above prog is executed, it gives compiler error. What is wrong in that code?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
suresh mit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one explain clearly on this.
 
Nareshreddy Kola
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
suresh mit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevi Deter. Now I have understood the difference between inner class and subclass.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always glad to know I've been able to help!
reply
    Bookmark Topic Watch Topic
  • New Topic