Inner Classes vs. Subclasses in Java

纵饮孤独 提交于 2019-12-04 09:40:26

There are big differences between inner classes and subclasses:

  • inner classes are in the same file, whereas subclasses can be in another file, maybe in another package.
  • You cannot get an instance of an inner class without an instance of the class that contains it.
  • inner classes have the methods they want, whereas subclasses have the methods of their parent class. Subclasses can of course define additional methods, but they'll always have those of their parent.

About the situation:

  • inner classes are used when your big class needs a (usually short) class, related to its internal operation, and when nobody else needs it. A good example Nik G quoted is the LinkedList: it needs a Node class to work, that is short, and that no other class needs. Therefore Node is an inner class of LinkedList.
  • subclasses are used when you defines a "is-a" reliationship. Picture this: you want to make different types of cars. They have common properties and features: they all can move, they all have passengers, etc. So you create an abstract class "Car" with these common things. And you create a subclass for every different type of car.

If you use inheritance, you define an "is-a" relationship between the two classes. That is, ChildClass is a ParentClass.

On the other hand, inner classes are not always directly related to the outer classes. The main reason to have an inner class is for the outer class to make use of its features without letting other classes know about it.

The best example I can think of off the top of my head is a LinkedList. It uses some sort of private Node class to store the contents of each element. Of course, a single Node is not a LinkedList... but a LinkedList can't work unless it it is made up of Nodes. There's also no reason for any class other than LinkedList to know that the Node class exists.

This is simple.. But inner class and sub class are not the same..

Sub classes uses when you have inheritance logic. For example Rectangle is Shape so: Rectangle can inherit from Shape

Inner classes are used when you have a class that you need to use only in particular class. That way no one will use the class unless he is in the class that need to use the inner class. For example: You have class A Class a has a Map. The Key class is class you created to define compound key class and it is used only inside of A. So Key can be inner class.. This way you can reduce files(Classes) so other developer won't need to handle and understand unless he is using A

Hope that make sense

A subclass inherits it's parent class' variables and methods, an inner class doesn't.

Therefor, you would want to use a subclass when the child class should have it's parent's members and use inner class when you only need it to perform it's part.

e.g you would use a an inner class named Node in a class named List so you can use Node as a member.

e.g you would use a subclass named Mercedes in a class named Car, as a Mercedes should inherit the members of a Car and override Car's methods if needed.

A subclass essentially shares an "is-a" relationship with its parent, whereas an Inner class shares a "has-a" relationship. For example, we have class yolk which is an inner class of Egg class, then we say Egg class "has a" yolk class. The object of yolk class may be as a member of Egg class, but there is no link of their feature. Also, say we have class Dog extends class Animal, so Dog class "is a" Animal class, and Dog class will inherit all features of Animal, and possibly have some more.

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