Classes and Encapsulation in C#
The class is the basic unit of modeling in C#. It bundles state (fields) and behavior (methods) into one type and decides what is visible from the outside and what stays hidden. An object is a concrete instance of a class on the heap: the class describes the shape, the object holds the values. Almost the entire rest of C#'s object model — inheritance, polymorphism, interfaces — is built on top of these ideas.
Encapsulation is not about "making fields private for the sake of it" — it is about preserving invariants: a constructor brings a new object into a valid state, access modifiers decide who may change that state, and abstraction hides details behind a narrow contract. The C#-specific traps: the compiler removes the default constructor as soon as you declare your own; this(...) and base(...) chain constructors in a strict base-first order; a member with no modifier is private, not public; an abstract class, unlike an interface, carries implementation and state. The layer-by-layer breakdown is below.
Topic map
- Classes and objects — a
classdescribes a reference type, an object is its concrete heap instance, and a variable holds a reference. - Constructors — what a constructor does at
new, why declaring your own removes the default constructor, overloading. - Constructor chaining —
: this(...)within the class,: base(...)to the base, base-first construction. - Access modifiers — six levels from
publictoprivate protected, and why a member defaults toprivate. - Abstraction — hiding implementation behind a public contract, properties instead of fields, abstract members.
- Abstract classes — an
abstractclass with state and implementation versus the pure contract of an interface.
Common Mistakes and Traps
| Mistake | Consequence |
|---|---|
Assuming b = a copies the object | For a class only the reference is copied — both variables mutate one heap object |
| Expecting a default constructor after your own | Declaring any constructor removes the implicit parameterless public one — old new T() stops compiling |
Confusing : this(...) and : base(...) | this targets a constructor in the same class, base targets the base class; swap them and you break the init chain |
| Leaving a field unmarked "as if public" | A class member with no modifier defaults to private, not public |
Confusing protected internal and private protected | The former is OR (wider), the latter is AND (narrower); a swapped conjunction opens or closes extra access |
| Treating an abstract class as "an interface by another word" | An abstract class carries fields, a constructor, and implementation and allows single inheritance only |
Trying to instantiate an abstract class with new | Compile error — an abstract type is instantiated only through a derived one |
Interview relevance
OOP basics come up on almost every C# interview — but the check is your model of types and their state, not memorized definitions. A candidate who explains a class as "a reference-type blueprint, while an object is a heap instance the variable holds a reference to" immediately stands out from "well, it's a template".
Typical checks:
- The difference between a class (definition) and an object (heap instance), and that a variable holds a reference.
- What a constructor does and when the implicit default constructor disappears.
- The meaning of
: this(...)versus: base(...)and the base-first construction order. - All six access modifiers and the
privatedefault for a class member. - Abstraction as a narrow public contract over hidden implementation.
- When to reach for an abstract class versus an interface.
Common wrong answer: "An abstract class is the same as an interface." That opens the discussion that an abstract class carries state, a constructor, and implementation, allows single inheritance only, and models "is-a", whereas an interface is a "can-do" capability contract that many unrelated types implement.