• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

anonymous (unnamed) Classes by default always folllows Overriding concept

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

my question is related to anonymous (unnamed) classes.
Anonymous classes are always sub-class of the some "new ClassName()". so my question is that if it is always sub-class of some class and if it is define the method that is already defined in super class then it is overriding the method. or we can say that it is by default follow the overriding principle.

I need your thoughts on this Topic.

Example Attached from SCJP Kathy Siera Book.



The Popcorn reference variable refers not to an instance of Popcorn, but to an
instance of an anonymous (unnamed) subclass of Popcorn.
Let's look at just the anonymous class code:


Regards,
Mahendra Athneria
Mumbai- India


[Nitesh: Added code tags. Please UseCodeTags]
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, they follow the rules of overriding and polymorphism: remember new fields or new static members count as hiding rather than overriding.

But your anonymous classes are in fact instances of the Popcorn class. Try executing this enhancement of the class you quotedThat demonstrates whether your Popcorn objects are instances of Popcorn, and what the class names are, and that you are in fact overriding the pop method. Because you cannot access the class name you cannot do a class cast on pp or ppp, so you cannot add methods.

And please maintain indentation inside the code tags.
 
Mahendra Athneria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your Answer.

I have one more question related to overriding and implementation.

Yesterday i faced one problem. i declared one interface with some functions. then i created implementer class who implement the interface. while implementing the methods i used @Override annotations. when i compiled then it gives error

method does not override a method from its superclass @Override

but i am overriding the method, and if i am overriding the method then why cant i use @Override annotations?

Thanks in Advance
 
Campbell Ritchie
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is strictly speaking correct; you are not overriding an interface method, but implementing it.
If you use the compiler from Java6, it won't give that error message.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic