Is there any technical difference between a class which inherits from another class and a class which is a subclass of it?
What's the difference between A and B in the following code:
A)
public class foo {
...
private class bar {...}
}
B)
public class foo { ...}
private class bar extends foo {...}
In the inheriting situation, you can treat the derived class like its base class. That is, you can keep a collection of base class references around and call their methods without needing to know whether the actual objects are of any derived type, and if yes which. This an important semantic concept.
In the member class case, the inner class is just an implementation detail of the outer class and not even visible to the consumer. Even it it were visible, it would just be one feature of the foo
class. You can see that the two situations are semantically very different, and you have to pick the one which represents your actual data model most accurately.
Technically, the derived class probably weighs a tiny bit heavier because of various virtual function calls, but the details depend on your language and the concrete situation.
You're mixing terms. A subclass is the same as an inherited class.
In example A, bar
is an inner class. An inner class is like a nested type. bar
can see all the private stuff from foo
but it's not itself a foo
(you can't cast bar
to foo
).
When using B) bar is also a foo. It inherits all member variables and methods from foo.
In A) bar has only access to foo's methods but it doesn't have them itself. It's no foo then.
In your example A) bar
is not a "subclass" - it is a nested class.
See this question for more information on what nested classes are used for: Why Would I Ever Need to Use C# Nested Classes
In A) class bar is not inherited from class foo.
In B) the class bar is a subclass, or an inherited class of foo. Subclass and inherited class are synonyms.