Nested classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Nested classes

What is the difference between nested classes and inherited classes? when use one and when the other?

9th Nov 2016, 1:23 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
5 Answers
+ 5
A type defined within a class or struct is called a nested type. For example: class Container { class Nested { Nested() { } } } Regardless of whether the outer type is a class or a struct, nested class types default is private when left undeclared, but can be set to be made public, protected or private. (Basically all the public-private types, etc. But if you don't state that it is public/protected, it automatically becomes private.) For inheritance, the class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.(Well not quite sure how to explain it simplier, go re-check back on the lessons) Basically the difference : - Nested class need not be be 'similar' to the outer class unlike inherited class - inherited class may not need to be inside a class to be consider a inherited class.
9th Nov 2016, 5:42 PM
Wen Qin
Wen Qin - avatar
+ 5
Maybe during a real world context you are creating a weapon for a game. You will have to set the weapon base damage and its random number generator for that, etc. That is where I will use nested class
9th Nov 2016, 10:51 PM
Wen Qin
Wen Qin - avatar
+ 2
class Person { class Hand { public void Shake() { Console.WriteLine("Hi"); } } }
13th Mar 2017, 6:56 PM
Vira Yanchuk
Vira Yanchuk - avatar
0
thanks, can you help me, for example when you choose to use nested class in a program, and not inheritence?
9th Nov 2016, 8:16 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
0
oke, now i get it i think, thank you.
10th Nov 2016, 4:20 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar