• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Instantiating abstract inner class

 
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been studying for the OCA 1Z0-803 exam and during a mock test I answered a question about the following code wrong. I know abstract classes can't be instantiated and I thought the same would apply for abstract inner classes but I was wrong. Can someone explain to me why you can instantiate an abstract inner class? Thanks for any help!

 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi j rab,

First of all, a warm welcome to CodeRanch!

j rab wrote:I know abstract classes can't be instantiated and I thought the same would apply for abstract inner classes but I was wrong. Can someone explain to me why you can instantiate an abstract inner class?


First of all, inner classes are not on the OCAJP certification exam. Secondly there is no difference between abstract top-level classes and abstract inner (nested) classes: both can not be instantiated. This is clearly illustrated in the following code snippetBoth line1 (top-level class) as line2 (inner class) will result in a compiler error because an abstract class can't be instantiated. It doesn't matter if it's a top-level or an inner class.

But now you are probably wondering why your code snippet compiles successfully Here's the answer: you don't have instantiated an abstract inner class (because that's not allowed as illustrated in the previous code snippet), but you have created an anonymous class and created an instance of this annonymous class. And again that's allowed for both top-level classes and inner classes. Here is again an illustrative code example using the same classesThis code snippet compiles successfully (without any compiler errors)! But although it seems you are instantiating abstract (inner) classes, that's not the case. And that's very important to notice (and remember)! On line1 you are creating an anonymous class of the (top-level) class Animal and instantiating this anonymous class, all in 1 statement. On line2 the same happens: you create an anonymous class of the (inner) class Engine and instantiate this anonymous class, again all in 1 statement. And that's exactly what you have done in your code snippet as well.

So this code snippet is equivalent to the above code snippet and illustrates what's actually happening behind the scenes

Hope it helps!
Kind regards,
Roel

Disclaimer: inner (nested) classes are not on the OCAJP certification exam, it's an exam objective of the OCPJP certification exam. So if you are preparing for the OCAJP certification exam, you don't have to worry about that weird looking syntax
 
j rab
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

That’s explains a lot! Thank you for your informative answer on this question. This question was on one of the mock exams from myexamcloud which had me confused a bit. Thanks again Roel!


 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

j rab wrote:That’s explains a lot! Thank you for your informative answer on this question.


Glad to hear my explanation was able to clear your doubts.

j rab wrote:This question was on one of the mock exams from myexamcloud which had me confused a bit.


If the code snippet from your original post was on one of the mock exams from MyExamCloud, you should definitely contact them and mention this question should not be in the OCAJP7 mock exams (as it's not an exam objective), it should be a question in the OCPJP7 mock exams instead. It's a mistake on their side! You can always refer to this topic if you report this errata item to MyExamCloud.

Kind regards,
Roel
 
j rab
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Roel.
reply
    Bookmark Topic Watch Topic
  • New Topic