• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes vs. HAS-A

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Suppose there are two classes: Dog and Collar.

In scenario #1, They are two separate classes. The Dog class HAS-A Collar instance.
In scenario #2, The Dog class contains the Collar class as a nested inner class.

So here is scenario #1:



And here is scenario #2:



What's the difference? In both cases, an instance of Collar has a special relationship with a particular instance of Dog. In other words, a specific Dog has a specific Collar.

What I am trying to get at is: what is the need/use of inner classes that cannot be accomplished by an aggregate (HAS-A) relationship between classes?
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Intresting question
I think one advantage is
Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is)
Waiting for more replies to this thread
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abhay,

As I see, there are two things different with inner classes:-
1. They can access private members of the out class (as pointed out by you)
2. They can inherit from a class different from the one that an outer class inherits from. So effectively we have a class inheriting from two classes, normally not available in Java.

However, I believe that these are side bonuses. What is the real reason for including inner class as a feature in Java? I wonder if anyone can give an example of some task which can be accomplished by an inner class but CANNOT be accomplished by any other way
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anonymous inner class can be one example where we can create a class and its associated methods on the fly.

for eg -


 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the real reason for including inner class as a feature in Java?


I think, the answer can be subjective, we might have to read up.

I've heard that inner classes are used to implement closures and callbacks. (Objects that maintain the information about their state and context and can be used from other code blocks)

Also, Nested Inner Class in an interface can be used to provide default implementation for the interface(I know you are not talking about Nested Inner Classes but I thought I'd share either ways).
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2. They can inherit from a class different from the one that an outer class inherits from. So effectively we have a class inheriting from two classes, normally not available in Java.


Are you thinking that an inner class has an IS-A (inheritance) relation with the outer class?? An inner class doesn't have an IS-A relation with the outer class unless you specify it.

What is the real reason for including inner class as a feature in Java?


Abhay gave a good example of the use of inner classes. Another use can be seen in the J2SE API. The java.util.Map interface has an inner interface calls Entry. That interface represents a single entry i.e. key value pair in the Map. It could have been defined as a regular interface, but defining it in the Map interface makes it more cohesive...
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monu Tripathi wrote:I've heard that inner classes are used to implement closures and callbacks. (Objects that maintain the information about their state and context and can be used from other code blocks)

Also, Nested Inner Class in an interface can be used to provide default implementation for the interface(I know you are not talking about Nested Inner Classes but I thought I'd share either ways).



Monu,
Thanks for your answer. I don't know much(anything!) about closures & callbacks, I'll see if I can find more about it.

Regarding your second point, I thought Inner Class and Nested Inner Class are the same thing. Isn't a class nested within another class called an inner class?

Since I had never heard of a default implementation for an interface, I tried this:

This ofcourse looks wrong (Default.method_1() is different from Inf.method_1()), and sure enough doesn't compile (Stub needs to override method_1). Could anyone give an example of default implementation of an interface using an inner class in interface?

Thanks,


 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:An inner class doesn't have an IS-A relation with the outer class unless you specify it.



You're right! I was fuzzy on this. Thanks for clarifying it for me

Ankit Garg wrote:Abhay gave a good example of the use of inner classes.



Thanks Abhay. I'm thinking, except for specialized code, inner classes would be mostly used as Abhay pointed out. To override methods on the fly using Anonymous classes.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Inner Class and Nested Inner Class are the same thing


There is a very small difference between the two. A non-static inner class is called inner class and static inner class is called nested class. But I don't think you need to worry about this, its very minute detail...
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't know much(anything!) about closures & callbacks, I'll see if I can find more about it.


I think the book, Thinking in Java has a section called Why Inner classes?, in a related chapter; find it here

My bad. I think I made a mistake there.
Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes. I was referring to static nested classes; I apologize for my careless typing.

What I meant by providing a default implementation is that you can have a class inside your interface that provides one version(implementation) of the interface methods. This default implementation would then be accessible to all the implementors of the method. That said, having an implementation inside something that is abstract sounds incorrect. Probably, it is a bad OO practice to have classes inside interfaces. But it can be done.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monu Tripathi wrote:I think the book, Thinking in Java has a section called Why Inner classes?, in a related chapter



I've seen this(Bruce Eckel) book before. It's really a great book. I think I must have missed this section in my previous reading of it. Time to re-read it.

Guys, thanks a lot for taking the time to answer my questions. It makes more sense now.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing Event Handler in swing and awt APIs are the best use cases for the inner class. If you define event handler for your button as inner class than you donot need to pass button attributes using setter getter methods, event handler will have access to these attributes automatically.

In swing sometimes you need to write a component specific handler that cannot be reused with other components, at that time inner class is the best solution.

Otherwise if you write outer event handler then may be you have to write so many outer classes for different types components on your GUI, sometimes more than one handler for a single button. This will result in lots of classes that will disorganize your project.

So in other way inner classes help you in organizing your project also.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic