Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson1 / 13

    Module 1 · The Four Pillars in C# · Lesson 1 of 13

    The Four Pillars of OOP in C#: Interview Answers

    What this lesson teaches

    This C# donation example demonstrates the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. You will learn how to explain each one in an interview.

    Object-oriented programming, or OOP, organizes code around objects that combine data with behavior. These four ideas help you decide what an object should protect, expose, reuse, and customize:

    • Encapsulation protects the donation amount: callers can read it, and construction rejects negative amounts.
    • Abstraction lets our loop call Describe() without inspecting whether the donation is cash or card.
    • Inheritance lets a class reuse and specialize a base class, expressing an “is-a” relationship.
    • Polymorphism means one call can run different code. With overriding, the real object decides when the program runs. With overloading, the compiler picks a method from the argument types.

    In an interview, say: “The four pillars are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation protects data and its rules. Abstraction exposes useful operations without making callers handle their details. Inheritance lets a derived class reuse and specialize a base class. Polymorphism lets the same operation behave differently for different objects. For example, cash and card donations share amount validation, hide their description details, inherit from Donation, and provide different descriptions through the same Describe() call.”

    We will build two kinds of donation. Both protect their amount, share a common description method, and produce different descriptions through the same call.

    By the end, you can explain the pillars, distinguish encapsulation from abstraction, and predict which method C# calls. This lesson assumes you know classes, properties, constructors, and basic inheritance from the C# Intermediate course: Protect Object State with Constructors and Methods, Properties, Init, and Immutability Boundaries, Interfaces and Composition and Inheritance and Polymorphism Responsibly.

    Classes, objects, and a complete example

    Before reading the program, a few terms will help:

    • State means the data an object currently holds. Here, that is the donation amount.
    • A field is a variable declared directly inside a class or struct. The private amount field stores our data.
    • A contract describes what callers can use and expect. Here, every donation must provide Describe().
    • An implementation is the code that performs an operation. Cash and card donations provide different description code.
    • An abstract class cannot be created directly and can require derived classes to supply missing members.
    • An override supplies a derived class's implementation of an inherited abstract or virtual member. A virtual member already has an implementation that derived classes may replace.
    • protected allows access from the declaring class and its derived classes. Here, derived constructors use the protected base constructor through base(amount). See Microsoft's access modifiers guide.
    • Overloads are methods with the same name but different parameter lists. The two Print methods below accept different parameter types.

    The : Donation declaration makes each specific donation inherit from Donation.

    Predict three things before running: the two descriptions, which Print overload each call chooses, and what happens to a negative amount.

    C#Runs
    using System;
    
    Donation[] donations =
    {
        new CashDonation(20m),
        new CardDonation(30m)
    };
    
    foreach (Donation donation in donations)
    {
        // donation.Amount = -5; // does not compile: Amount has no setter
        Console.WriteLine(donation.Describe());
    }
    
    Donation declaredAsDonation = new CardDonation(40m);
    Reporter.Print(declaredAsDonation);
    Reporter.Print(new CardDonation(50m));
    
    try
    {
        new CashDonation(-1m);
    }
    catch (ArgumentOutOfRangeException)
    {
        Console.WriteLine("Rejected negative amount.");
    }
    
    public abstract class Donation
    {
        private decimal amount;
        public decimal Amount => amount;
    
        protected Donation(decimal amount)
        {
            if (amount < 0)
                throw new ArgumentOutOfRangeException(nameof(amount));
    
            this.amount = amount;
        }
    
        public abstract string Describe();
    }
    
    public class CashDonation : Donation
    {
        public CashDonation(decimal amount) : base(amount) { }
        public override string Describe() => $"Cash donation: {Amount}";
    }
    
    public class CardDonation : Donation
    {
        public CardDonation(decimal amount) : base(amount) { }
        public override string Describe() => $"Card donation: {Amount}";
    }
    
    public static class Reporter
    {
        public static void Print(Donation donation)
            => Console.WriteLine("Donation overload");
    
        public static void Print(CardDonation donation)
            => Console.WriteLine("CardDonation overload");
    }
    Output
    Cash donation: 20
    Card donation: 30
    Donation overload
    CardDonation overload
    Rejected negative amount.

    The array holds two objects. Each has its own amount, even though both are accessed through the Donation type.

    The mistake to avoid. Do not name “class and object” as two pillars. They are the building blocks used to demonstrate the four pillars.

    In an interview, say: “A class is a blueprint that describes data and behavior. An object is an instance of that class in memory. CardDonation is the class; each new CardDonation(...) creates an object with its own amount.”

    Encapsulation: protect the donation amount

    Our rule is simple: a donation amount must not be negative. Zero is allowed in this example.

    The private field prevents callers from assigning the amount directly. The public Amount property allows reading but provides no setter. If you uncomment donation.Amount = -5; in the program, the compiler blocks the assignment because Amount has no setter. Construction is the only place this class assigns its amount, and it checks the rule first.

    This is more than adding private. Encapsulation puts the rule next to the data and makes callers go through the checked path. If we later add a method that changes the amount, that method must preserve the same rule.

    Microsoft's architectural principles describe this as limiting outside access to internal state and providing well-defined operations instead.

    For “Which pillar do you rely on most?”, choose one and give a concrete story. Encapsulation is a useful answer when your work involves rules that many callers must obey. Adapt the following example to something you have actually built.

    The mistake to avoid. A public setter that accepts any amount would let callers bypass the rule. A private field alone does not help if another public member permits invalid changes.

    In an interview, say: “I rely heavily on encapsulation. For example, in a donation feature, I would prevent callers from setting an amount directly and reject negative amounts during construction. That keeps the rule in one place instead of requiring every screen or service to remember it.”

    Abstraction: expose the operation, not its details

    Abstraction gives callers a small, useful view of an object. The caller needs to describe a donation. It does not need to know how each kind builds its description.

    The loop does not inspect whether a donation is cash or card. It asks each object for a description.

    An interface describes a set of members that implementing types provide. Interfaces, abstract classes, and small public APIs can all express abstractions. Abstraction is the design idea, not a synonym for the abstract keyword.

    This example also answers “encapsulation versus abstraction” in one class:

    In DonationPurpose
    Private amount plus the constructor checkEncapsulation protects valid state.
    Public Describe() contractAbstraction lets callers request a description without knowing its implementation.

    They work together, but solve different problems. Protecting the amount would still matter if there were only one donation kind. Hiding description details matters to the loop regardless of where the amount is stored.

    The mistake to avoid. Do not reduce the distinction to “private versus abstract.” Making things private helps encapsulation. A clear public operation that hides its details is what makes an abstraction.

    In an interview, say: “Encapsulation protects an object's state. Abstraction hides complexity behind a useful contract. In Donation, the private amount and constructor check protect the data, while Describe() lets callers obtain a description without knowing the formatting details.”

    Inheritance: specialize a genuine is-a relationship

    A cash donation is a donation. A card donation is also a donation. That makes a shared base class a reasonable choice for this example.

    The constructor passes the amount to Donation, which performs the shared check. CashDonation does not need a second copy of that validation. It specializes the description instead.

    flowchart LR
        C["CashDonation"] -->|"is a"| D["Donation"]
        K["CardDonation"] -->|"is a"| D

    Both derived classes can be used where the program expects a Donation.

    Inheritance also creates a dependency: changes to the base class can affect its derived classes. Do not introduce a base class merely because two methods contain similar code.

    Composition means building a class from other objects that it holds and calls. Prefer it when the relationship is “has-a” rather than “is-a.” A donation receipt can hold a donation; it is not itself a donation.

    See Microsoft's inheritance guide for the language rules.

    In an interview, say: “Inheritance lets a class reuse and specialize a base class. Here, a cash donation is a donation: it reuses amount validation and supplies its own description. I would use composition instead when one object holds another as a part or helper, without being that type.”

    Polymorphism: compile time or run time?

    Both answers can be correct, but they describe different mechanisms.

    Compile time is when the compiler checks and translates your code. Run time is when the program executes.

    The declared type is the type written in a declaration, such as Donation in Donation declaredAsDonation = new CardDonation(40m);. The actual type is the type of the object created with new, here CardDonation.

    For overrides, the actual object's type determines which implementation runs. In the loop, the variable is declared as Donation, but its objects are a CashDonation and a CardDonation. Their respective Describe() overrides execute.

    For ordinary overload calls like these, the compiler selects a method using the declared argument types:

    C#
    Donation declaredAsDonation = new CardDonation(40m);
    Reporter.Print(declaredAsDonation);
    Reporter.Print(new CardDonation(50m));

    The first argument has declared type Donation, so the first call selects Print(Donation). Holding a card object does not make it select Print(CardDonation).

    The second argument is a CardDonation expression, so that call selects the more specific Print(CardDonation) overload.

    Ordinary methods are not virtual, so a derived class can override only what the base marks virtual or abstract. Lesson 4 covers virtual, override and new in full.

    The mistake to avoid. Do not say that every method choice follows the actual object. The first Print call still selects the Donation overload, despite receiving a card donation object.

    In an interview, say: “Both. Overloading is commonly called compile-time polymorphism: for ordinary calls, the compiler chooses using declared argument types. Overriding provides run-time polymorphism: a call through the base type executes the override belonging to the actual object's type.”

    Is C# a pure object-oriented language?

    In an interview, say: “No. C# supports object-oriented programming, but it also has static members and value-type structs that cannot inherit from a user-defined base class. It also supports functional features such as lambdas and LINQ.”

    Static members belong to the type, not to an individual object. You can call a static method without creating an object of its class. A struct is a value type, so assigning a struct variable copies its data. Structs cannot inherit from a user-defined base class, though they can implement interfaces.

    A lambda is an unnamed function. LINQ provides ways to query data in C#. These features let C# combine object-oriented and functional styles. The overview of object-oriented techniques describes C#'s broader set of type features.

    Check yourself

    A variable declared as Donation holds a CardDonation. What happens when the program calls Reporter.Print(variable) and variable.Describe()?

    A Both calls choose the CardDonation-specific method at run time.
    B Print(CardDonation) is selected, and Donation.Describe() executes.
    C Neither call compiles because Donation is abstract.
    D Print(Donation) is selected, and CardDonation.Describe() executes.
    Show answer

    Correct answer: D

    The ordinary overload call uses the argument's declared type, Donation. The overridden Describe method follows the actual CardDonation object. An abstract type can be used for a variable even though it cannot be created directly.

    Which statement best distinguishes encapsulation from abstraction in the donation example?

    A Encapsulation selects overrides at run time, while abstraction selects the overloads.
    B Encapsulation guards the amount, while abstraction hides how Describe builds its text.
    C Encapsulation requires inheritance, while abstraction requires private fields.
    D Encapsulation marks Describe abstract, while abstraction makes the amount private.
    Show answer

    Correct answer: B

    Encapsulation protects state and its rules. Abstraction exposes useful operations while leaving their implementation details out of the caller's code.

    Going deeper

    Why do some Microsoft pages list three pillars? Microsoft's inheritance documentation names encapsulation, inheritance, and polymorphism as three primary characteristics. The common four-pillar interview answer adds abstraction. In an interview, name all four.

    Why an abstract class here? It gives the two donation kinds both a required operation and shared amount validation. An interface can describe capabilities across otherwise unrelated types. A class can implement multiple interfaces but has only one direct base class.

    Where this course goes next

    Lessons 2–7 cover the C# language features behind these ideas. Lessons 8–12 cover SOLID, five design principles for organizing responsibilities and dependencies. Lesson 13 brings the ideas together in a refactor: changing code structure while preserving its behavior.

    Sources

    Practice

    Practice test: Practice: The Four Pillars of OOP in C#: Interview Answers8 questions · 1 mark each, no negative markingFull Access
    Take the test
    Sign in to mark lessons done and keep your place in the course.Sign in

    Checked before publishing: C# examples run on .NET 10, links opened, reviewed while it was written and read in full. How lessons are checked

    Updated 30 Nov 2026 ·