• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Inner Class  RSS feed

 
Shreyas Dange
Greenhorn
Posts: 16
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying out inner classes demo when I came around this piece of code.


I am defining "eat()" method in outer as well as in anonymous class. when I try to call obj.eat() it call eat() method of the anonymous class always. What can be done to call eat() method of AnonymousClsDemo.

Looking forward for you replies.

Thanks in advance.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, the object referenced by obj is an instance of a subclass of AnonymousClsDemo. This subclass (an anonymous class i.e. a class with no name) overrides (replaces) the public void eat() method inherited from its superclass AnonymousClsDemo.

You can create an instance of AnonymousClsDemo and call its eat() method as follows.

 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shreyas, I think the answer is in your question itself "What can be done to call eat() method of AnonymousClsDemo?". Well.. how about creating a new object of this class AnonymousClsDemo and calling its eat method directly?

Edit- Just saw Joe's answer, I am saying the same thing as him.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shreyas Dange wrote:What can be done to call eat() method of AnonymousClsDemo?

Two possibilities:
1. Change AnonymousClsDemo to define:
  public final void eat() { ...

2. Change 'obj's implementation to:
  public void eat() {
    super.eat();
  }

HIH

Winston
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston, I don't think option 2 will work since we can't call super from a static context( public static void main).
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not calling it from a static context.
You're calling it from the eat() method.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Sagar wrote:Hi Winston, I don't think option 2 will work since we can't call super from a static context( public static void main).


The context is the subclass' eat() method (although the subclass is anonymous), which is not a static method. How is this call a static context?

EDIT: Wow. Beaten to the answer by less than a minute... have a cow ...

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Change AnonymousClsDemo to define:
public final void eat() { ...
but you can't have eat method in anonymous class because final method can't be overridden.

Yes
Change 'obj's implementation to:
public void eat() {
super.eat();
}

This works as it calls eat method of AnonymousClsDemo as AnonymousClsDemo is super class of anonymous subclass. Like this


Output:
Eating Outer
Eating Inner

Another way is by making AnonymousClsDemo eat method access modifier as private.

Output: Eating Outer
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:but you can't have eat method in anonymous class because final method can't be overridden.

But that wasn't the question.

Another way is by making AnonymousClsDemo eat method access modifier as private.

Hmmm. I'd say that's even more drastic than my first suggestion because it changes the entire API of the class.

Winston
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
But that wasn't the question.

"Shreyas Dange wrote:
I am defining "eat()" method in outer as well as in anonymous class.
I felt he wants to have eat method in both class and still wants to call super class method.
Winston Gutkowski wrote:
I'd say that's even more drastic than my first suggestion because it changes the entire API of the class.
yes, your correct...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!