wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes vs. HAS-A Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes vs. HAS-A " Watch "Inner classes vs. HAS-A " New topic
Author

Inner classes vs. HAS-A

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252



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?


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1086
    
    1

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

Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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

Joined: Feb 29, 2008
Posts: 1086
    
    1

Anonymous inner class can be one example where we can create a class and its associated methods on the fly.

for eg -


Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

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).

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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

Joined: Oct 19, 2009
Posts: 252

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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Oct 12, 2008
Posts: 1369
    
    1

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

Joined: Oct 19, 2009
Posts: 252

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.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes vs. HAS-A