Announcing Stack Overflow Documentation



Sign up and start helping → Learn more about Documentation →

The inner class is always inside the outer class and if we remove outer class then the inner class would also be destroyed. I don't think about memory release, I am only thinking about the concept. And this is same concept about the composition. Then the inner class must be the example of composition relationship of objects in the real world.

In short I want to relate the concept of an inner class to the real world Object Orientation.

I think the constructor is the good example of composition then what is the basic need to involve inner class in Object Orientation.

I have idea about inner class but so much ambiguity.


University is the Outer class.

Faculty is the inner class.

I do understand Object Orientation, but I find it difficult to relate the inner class to the real world. Could you give me some pointers?

share|improve this question

closed as not a real question by musiKk, adarshr, Brian Roach, Donal Fellows, Joris Meys Apr 4 '11 at 16:21


In short I want to relate inner class to the real world Object Orientation.

The expression "Real world Object Orientation" does not make much sense since the world is not 100% object oriented. Object Orientation is a simplification, it is a means to create simplified models of real world systems.

Putting that aside, you want examples in real life? C'mon, are you kidding me. It's not that hard. Consider your first sentence which already gives you an idea of how it would work in real life:

The inner class is always inside the outer class and if we remove outer class then inner class would also be destroyed.

Classes are not destroyed. Objects, instances of those classes are. You need to get your definitions right. I'm assuming you understand the concept of classes and objects, right? If not, I suggest you stop now and clear those up. Moving on.

Second, outer and inner classes do not necessarily mean composition. An instance of an inner class can exist and not be destroyed when the outer class enclosing object instance is destroyed.

Real life example of Inner/Outer Classes that DO NOT relate in Composition:

A Nissan 240SX car is an instance (a specific car) of that model (the Outer class). It's engine (which specific for that model) is a specific object or instance of the Nissan 240SX engine (the inner class).

The car (the object instance of the outer class) gets in an accident and is mostly destroyed, but the engine (the object instance of the inner class) is salvaged (it is not destroyed.)

Real Life Example of Inner/Outer Classes that relate in Composition:

Heart (or any internal organs) - inner class Body - outer class.

If an actual body (an object instance of the outer class), its heart (its object instance of the inner class that happens to relate in composition to the enclosing body) dies as well.

Real Life Example of Composition that does involve Inner/Outer Classes:

Bank Account - enclosing class Transaction - enclosed class

A transaction is simply some money and an action type (debit, credit). It is too simple to warrant special treatment or manufacturing from a bank account.

You close (destroy) a bank account, and all transactions within it also get destroyed.

Obviously they do not get destroyed as a bank will keep records of a closed account (and all activity in it), but from the point of view of the owner of the account, the account and its internals get closed/destroyed.

What is Composition?


What is an Inner Class?


One More Thing:

Google is your friend. If you really want to get somewhere as an engineer, you better learn how to do your own research.

share|improve this answer
I am going to work on this more deeply then I'll give you response. By the way thanks for all of this. Last one is superb. Thanks again – userBI Apr 3 '11 at 13:13
No prob R.K. Just persevere and keep going at it. It takes time to really get to understand all these concepts. – luis.espinal Apr 3 '11 at 14:54
Also, you better forget about the 1-to-1 correspondence between OO models and real-world models. There is no 1-to-1 correspondence as the world is far more complex and cannot be fully modeled or analyzed with a OO approach. The world is multi-paradigm, modular and, in many cases, procedural. OO models are artificial constructions we make to better analyse the world. We typically do not pretend they are real or that have 1-to-1 correspondences with the real world. – luis.espinal Apr 3 '11 at 15:04
@R.K. I think the best way to see this is as follows: Given two classes, A and B, a) does class B help class A in developing its logic (or does class B help a user of class A use class A), AND is the internal structure of B highly dependent on the structure of class (or the structure of class A highly depends on class B to achieve some quality or purpose)? If you answer yes to both questions, then class B is a good candidate to be an inner class of A. Otherwise, it is very difficult to justify an inner/outer relationship. – luis.espinal Apr 3 '11 at 15:05
@R.K. One more thing, when it comes to composition, a class A is a composite (containing another class B) if A is a) completely in charge of creating and destroying instances of B, and a instance of B, if it ever exists, it always does within one and only one instance of A. – luis.espinal Apr 3 '11 at 15:17

Distinguish between Classes and Objects. Define a Class

Class Truck extends Vehicle {

    private SteeringWheel mySteerer;


Here we see that when we create a Truck Object it has-a SteeringWheel. A composition relationship. Now where is the Class definition of the SteeringWheel? So far you don't know.

It could be in a separate Class file:

Class SteeringWheel {
     // stuff

Or alternatively we could make it an inner class:

Class Truck extends Vehicle {

    private SteeringWheel mySteerer;

    private Class SteeringWheel {
        // some stuff


In either case we still have Composition. The thing that the Inner Class is giving us is structure in the code, by making it an Inner Class we restrict the scope of the SteeringWheel class, it's only visible in the Truck class.

So Inner Classes are primarily a tool for orgainsing the implementation - good code organisation aids maintenace.

share|improve this answer
Note that the inner class in this example holds an implicit reference to the containing class, which could have a number of unintended side effects. – CurtainDog Apr 3 '11 at 12:40
ya, in your example you do the composition with the help of inner class then whats the matter. Why inner class don't do the composition. I think that If we do the composition with inner class then we get the actual benefit from the inner class. Till now I have little bit ambiguity. – userBI Apr 3 '11 at 12:50

The answer is, it has nothing to do with "real world object orientation", whatever that is. It is a just a programming language feature. There could be object oriented languages that would not support inner classes - just like there are programming languages that support inner functions/procedures and others do not.

share|improve this answer
no, I don't agree with you. I think that every concept in Object Orientation is come from real world concept. Just wait I will find out after some hour or some day. Then I'll show you, How is it relate to real world concept. – userBI Apr 3 '11 at 12:36
"I think that every concept in Object Orientation is comes from real world concepts." - this is either trivial or meaningless. But we try to tell you that inner classes are no OO concept at all. – Ingo Apr 3 '11 at 12:41
sorry I didn't mean to hurt you. But when I visualize the concept of Object Orientation then I relate all of the concept to real world situation. So I.... – userBI Apr 3 '11 at 13:12
@R.K. - you do not say to someone that he's wrong, but take a time to get the evidence to prove it. Maybe in politics, but not in engineering. You either know it, or do not. Besides, Ingo is right. You should listen to people with actual work experience. OO is nothing new, more than 40 years old, and we have known for several decades ago that models in the real world are not necessarily orthogonal to object oriented models (the later, artificial fabrications we create to better deal with the complexities of the real world.) – luis.espinal Apr 3 '11 at 15:02
