Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays Encapsulation

Marina Gulakova
Marina Gulakova
3,145 Points

If one class inherits another class, can it use inherited class' private methods, fields, constructor?

This question in regards to a quiz question: If all parts of a class (fields, methods, constructor, etc) are declared private is there any way for other classes to use this class?

So, the correct answer is No. However, if another class inherits this class, then it could use all the private fields, methods, constructor, etc., couldn't it? If so, then the correct answer is Yes.

1 Answer

Steven Parker
Steven Parker
231,269 Points

No, private fields cannot be used by inheritance.

That's what "protected" is for. It allows use through inheritance, but it's not public.