[ACCEPTED]-Is there any technical difference between a subclass and an inherited class?-inheritance

Accepted answer
Score: 17

You're mixing terms. A subclass is the same 4 as an inherited class.

In example A, bar is 3 an inner class. An inner class is like a nested type. bar can 2 see all the private stuff from foo but it's 1 not itself a foo (you can't cast bar to foo).

Score: 2

In the inheriting situation, you can treat 19 the derived class like its base class. That 18 is, you can keep a collection of base class 17 references around and call their methods 16 without needing to know whether the actual 15 objects are of any derived type, and if 14 yes which. This an important semantic concept.

In 13 the member class case, the inner class is 12 just an implementation detail of the outer 11 class and not even visible to the consumer. Even 10 it it were visible, it would just be one 9 feature of the foo class. You can see that 8 the two situations are semantically very 7 different, and you have to pick the one 6 which represents your actual data model 5 most accurately.

Technically, the derived class probably 4 weighs a tiny bit heavier because of various 3 virtual function calls, but the details 2 depend on your language and the concrete 1 situation.

Score: 0

When using B) bar is also a foo. It inherits 4 all member variables and methods from foo. In 3 A) bar has only access to foo's methods 2 but it doesn't have them itself. It's no 1 foo then.

Score: 0

In your example A) bar is not a "subclass" - it 2 is a nested class.

See this question for more information 1 on what nested classes are used for: Why Would I Ever Need to Use C# Nested Classes

Score: 0

In A) class bar is not inherited from class 3 foo.

In B) the class bar is a subclass, or 2 an inherited class of foo. Subclass and 1 inherited class are synonyms.

More Related questions