简体   繁体   中英

Inner Classes vs. Subclasses in Java

Is there a situation where it is more beneficial to use an inner class over a subclass in Java (or vice-versa)? Based on my current understanding, inner classes have access to the fields and methods of the outer class. How is this any different from using inheritance?

Subclasses generally have access to all the fields/methods labeled public and protected. Fields labeled private in the parent class can be accessed in the subclass using a getter method. Based off what I've seen thus far, when methods are labeled private, they're usually called in other methods of the class that are labeled either public or protected. Granted, I'm not an experienced Java programmer, but that seems to be the general trend.

Based on my current understanding, there seems to really be no benefit between choosing one over the other. Can someone give insight as to why and when I should use an inner class over inheritance (or vise versa)?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM