• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java Inner Class

 
Greenhorn
Posts: 16
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not calling it from a static context.
You're calling it from the eat() method.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic