Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Anonymous Classes

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Just read about anonymous classes from website but it was not sufficient. Just a little confused abt the question posted in www.examulator.com the question goies like this....

Which of the following statements are true?

1 An anonymous class cannot be inherited
2 An anonymous class may extend another class
3 An anonymous class may not create and start a Thread
4 An anoymous class may not declare a constructor



the 1,2,4 answers are right... please let me know why ???
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) class X extends Y
But Y has no name so how would you call it???
2)ANY class may extend another class
4) How would you call it? Constructors have the same name as the class but the class has no name???
Hypothetical code, this won't compile

that's the reasons
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could some one tell me if the following is true?
1. An annoymous class MUST either extend another class or implement an interface?
2. Any new (i.e. not defined in the superclass or interface) method is not acessable. If this statement is false, how would one access it?
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. An annoymous class MUST either extend another class or implement an interface?


First of all, every class in Java (with the exception of Object) extends another class in that all classes extend Object implicitly. In the case of an anonymous class, however, it must extend something because, in order to create the class, you must write new "something." That "something" might be Object but, in one way or another, an anonymous class is necessarily going to extend something or implement something.


2. Any new (i.e. not defined in the superclass or interface) method is not acessable. If this statement is false, how would one access it?


Basically, they are inaccessible because we don't know the name of the anonymous class instance. However, I imagine you could actually invoke such methods using reflection, but that's well beyond the scope of the SJCP exam.
I hope that helps,
Corey
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic