13

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)?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Joel J.
  • 425
  • 1
  • 5
  • 14
  • 1
    You cannot extend 3rd party code through inner classes. – ergonaut Oct 29 '15 at 18:02
  • 2
    Type "Inner Classes vs. Subclasses in Java" and see what happens – user1231232141214124 Oct 29 '15 at 18:02
  • An inner class has nothing to do with a subclass. – Tobi Oct 29 '15 at 18:02
  • You use inheritance if you like to _extend_ a parent class or where a _"is a"_ relation exists. This is not necessarily the case for inner classes. They can (for example) be used as small helper classes, like in `Map` implementations, there the `Entry` class is nested. – Tom Oct 29 '15 at 18:03
  • There's too many differences between inner classes and subclasses to count, and they go far beyond the obvious "is-a" vs. "has-a" problem. I suggest you continue studying about the two if you think they're interchangeable with no "benefits" against each other. In reality, that couldn't be further from the truth. – Shashank Oct 29 '15 at 18:39
  • 1
    @redFive: I did so and got this page. :-) – LarsH Aug 19 '16 at 13:44

5 Answers5

19

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.
ElectronWill
  • 754
  • 9
  • 17
  • 1
    +1, this is helpful. However I'm not sure your second bullet is actually a difference. Isn't it true that if you have an instance of a subclass, then you have an instance of the parent class? – LarsH Aug 19 '16 at 13:47
  • If class B is an inner class of A, you cannot create an instance of B without an instance of A. You have to create an A first. It's different with subclasses: if D is a subclass of C, you can create an instance of D without creating an instance of C first. What you can do is later _cast_ D to C or _assign_ D to a C field. – ElectronWill Aug 22 '16 at 19:21
  • What I meant was that if B is a subclass of A, and you create an instance of B called `b`, then `b` is also an instance of A. But I think we're in agreement about the facts, just using different words. – LarsH Aug 23 '16 at 14:16
5

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.

Mage Xy
  • 1,803
  • 30
  • 36
  • I think your first point is key ... an inner class doesn't need to "be a" type of its enclosing class. The part about other classes not knowing about the inner class is often true, but there are prominent exceptions. E.g. Android's RecyclerView.Adapter is an inner class of RecyclerView (I think... correct me if I'm wrong!), but it's for public use. – LarsH Aug 19 '16 at 14:27
1

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

Aviad
  • 1,539
  • 1
  • 9
  • 24
1

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.

MrBlueSky
  • 305
  • 3
  • 10
-2

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.