Reputation: 425
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)?
Upvotes: 13
Views: 28371
Reputation: 1803
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.
Upvotes: 5
Reputation: 823
There are big differences between inner classes and subclasses:
About the situation:
Upvotes: 19
Reputation: 455
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.
Upvotes: -1
Reputation: 305
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.
Upvotes: 1
Reputation: 1549
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
Upvotes: 1