This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

an interview question  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my friend had this question in interview :
what is the difference b/w inner class & nested class .

well , i just read a chapter of inner class from K&B cirtification book and i think the answer is : there is no difference b/w inner class & nested class but if we mark inner class as a static then it would be called as a top level nested class it would not be technically an inner class .

is the answer is correct ???

after this if suppose interviewer ask that why they are technically not be called as a inner class afterall it is inside the curly braces of outer class .

then what should be the answer ???

please reply ...

thanks a lot
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose interviewer ask that why they are technically not be called as a inner class afterall it is inside the curly braces of outer class.


An instance of a static nested class is not associated with an instance of an outer class.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what is the difference b/w inner class & nested class

Semantics. A nested class is any class that is defined within another class (i.e. it is a member of another class). An inner class is just a non-static nested class.

Why knowing this distinction (rather than, say, understanding the access difference between a static and non-static inner class) has any bearing on being able to engineer software using Java is the really hard question...

[ January 12, 2005: Message edited by: Paul Sturrock ]
[ January 12, 2005: Message edited by: Paul Sturrock ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well , i just read a chapter of inner class from K&B cirtification book and i think the answer is : there is no difference b/w inner class & nested class but if we mark inner class as a static then it would be called as a top level nested class it would not be technically an inner class .

is the answer is correct ???


Well from what you just wrote there is a difference, technically, so I'm not sure what you mean.

Note that the egregiously silly term "top-level nested class" was removed from Sun's terminology several years ago when JLS second edition came out. (Unfortunately not everyone realized this right away, including K & B.) The terms "top-level" and "nested" are mutually exclusive. All classes are either top-level or nested (not both). All nested classes are either static nested classes (also known as static member classes), or inner classes (any nested class that's not static). Inner classes are either non-static member classes, local classes, or anonymous classes.

Why knowing this distinction (rather than, say, understanding the access difference between a static and non-static inner class) has any bearing on being able to engineer software using Java is the really hard question...

Well, if you ever consult the Java Language Specification, it's very helpful to have a correct understanding of the terms as the JSL defines them and uses them. The JLS uses terminology very precisely, and programmers need to do so as well in order to understand it correctly.

Having said that, I think the original question makes a rather poor interview question, because many otherwise excellent programmers would answer incorrectly due to the large amount of contradictory information which can be found in Java books. Sun made a mess of this by not using their terminology consistently when they first introduced nested classes (not in the JLS - it was a document known both as the Inner Classes Specification, and the Nested Classes Specification, which indicates the basic problem). They've cleaned up their mess since then, mostly, but there are still many people who learned different terminology.
[ January 12, 2005: Message edited by: Jim Yingst ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, if you ever consult the Java Language Specification, it's very helpful to have a correct understanding of the terms as the JSL defines them and uses them. The JLS uses terminology very precisely, and programmers need to do so as well in order to understand it correctly.

OK. I concede my response was somewhat coloured by what I see as a irritating type of interview question: the type which tries to find out if you can remember the JLS off by heart (something which someone with no real grasp of programming, but a lot of time could do), rather than a more useful one which tries to find out if you understand how making an inner class static affects how it can be accessed. I've also never liked describing an inner class using a different name simply because its been made static. I can't help but feel that just makes it difficult for beginners.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!