Does an inner class work as a composition relationship in java? [closed]

给你一囗甜甜゛ 提交于 2019-12-19 12:22:23

问题


The inner class is always inside the outer class. If we removed the outer class, the inner class would also be destroyed. I'm not concerned about memory release, I am only thinking about the concept. And this is the same concept as composition. IIn this case, the inner class must be the example of a composition relationship of objects in the real world.

In short, how can I relate inner classes to object orientation in real life? By this, I mean a non-technical example; perhaps a metaphor related to animals or video games?

If constructors are a good example of composition, then what is the basic need to involve inner classes in Object Orientation?

Are the examples below accurate?


E.g

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?


回答1:


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.

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?

Wikipedia Link

What is an Inner Class?

Wikipedia Link

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.




回答2:


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 a 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 organizing the implementation - good code organization aids maintenance.




回答3:


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.



来源:https://stackoverflow.com/questions/5529454/does-an-inner-class-work-as-a-composition-relationship-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!