views:

242

answers:

3

Please retag this question to include languages to which it is relevant

So my java book had a whole chapter on nested classes, but ended on the note that you should only really use them when it comes to "modeling composition relationships and implementing internals of a class you want to hide". So lets discuss when you would want to use nested classes and some examples.

+2  A: 
Am
+2  A: 

A nested/inner class is just a class that's only ever used specifically in the context of another class, which doesn't have it's own class file. If it's linked to an instance, it can only be instantiated in the context of a parent class instance; it can see private data, or only private static data if it's a static class.

The java developer site has a nested classes tutorial with one example: http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

A couple examples of usage:

  • Hide a concrete implementation of an interface:

(Thinking of a database session for a tool like Hibernate): Suppose you have a Session interface, and a SessionFactory which returns an instance of a Session. The SessionImpl concrete class that implements the Session interface could be an innner class of the factory that knows how to construct and initialize it.

  • Supply information by implementing an interface:

In the Wicket web framework, each GUI component has an associated "model", whose job is to wire data to the component. The interface looks something like:

public interface IModel extends IDetachable {
 public Object getObject();
 public Object setObject();
}

Suppose you have some special logic to retrieve data for a custom GUI component that you've written. Since no other component retrieves data the same way, you could use an anonymous class at the point where the IModel is supplied to take care of the data retrieval. If you have another point in the same class where you need to reuse your IModel implementation, you could make it an inner class. Later, if you need the model elsewhere, you could convert it to a top-level class.

Generally you use an inner class in a situation where you need a class definition, but that class is only usable or only makes sense in the context of the parent class.

RMorrisey
+2  A: 

I use nested classes for encapsulating algorithms that would be usually done as a method with lots of arguments. I use class that has raw data and I put algorithms into separate file in nested class (using partial keyword). That way I can put properties for that algorithm and its (working) data lives after algorithm is done. I know that can be easily done without nested classes but this feels right because algorithm is purposely built for parent class.

   public partial class Network
    {
            partial void initFDLF()
            {
                fdlf=new FDLF(this);
            }

        public FDLF fdlf;
        public class FDLF
        {
            internal bool changed=true;
            internal bool pvchange=true;
            public double epsilon = 0.001;
            public bool fdlfOk=false;
            public void init(){...}
            public void run(){...}
            ...
Domagoj Peharda