Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Base Keyword Behavior
📖 Scenario: Imagine you are creating a simple program to understand how a child class can use and extend the behavior of its parent class in C#.
🎯 Goal: You will build two classes: a parent class called Animal with a method Speak(), and a child class called Dog that overrides Speak() but also calls the parent class's Speak() method using the base keyword.
📋 What You'll Learn
Create a class Animal with a method Speak() that prints "Animal speaks"
Create a class Dog that inherits from Animal
Override the Speak() method in Dog to first call base.Speak() and then print "Dog barks"
Create an instance of Dog and call its Speak() method to see both messages
💡 Why This Matters
🌍 Real World
Understanding how child classes can reuse and extend parent class behavior is key in many software designs, like creating different types of animals, vehicles, or user roles.
💼 Career
Many programming jobs require knowledge of inheritance and the <code>base</code> keyword to write clean, reusable, and maintainable code.
Progress0 / 4 steps
1
Create the Animal class with Speak method
Create a class called Animal with a public method Speak() that prints "Animal speaks".
C Sharp (C#)
Hint
Use class Animal and inside it define public void Speak() that uses Console.WriteLine.
2
Create Dog class inheriting Animal
Create a class called Dog that inherits from Animal.
C Sharp (C#)
Hint
Use class Dog : Animal to inherit from Animal.
3
Override Speak method in Dog and call base.Speak()
In the Dog class, add a public method Speak() that calls base.Speak() and then prints "Dog barks".
C Sharp (C#)
Hint
Use public new void Speak() in Dog, call base.Speak(), then print "Dog barks".
4
Create Dog instance and call Speak
Create an instance of Dog called dog and call dog.Speak() to print both messages.
C Sharp (C#)
Hint
Create Dog dog = new Dog(); and call dog.Speak(); inside Main().
Practice
(1/5)
1. What does the base keyword do in C#?
easy
A. It creates a new instance of a class.
B. It allows a child class to access members of its parent class.
C. It defines a new class.
D. It deletes an object from memory.
Solution
Step 1: Understand the role of base
The base keyword is used in a child class to refer to its parent class members.
Step 2: Identify what base allows
It allows access to parent class methods, properties, or constructors from the child class.
Final Answer:
It allows a child class to access members of its parent class. -> Option B
Quick Check:
base keyword = access parent members [OK]
Hint: Remember: base = parent class access [OK]
Common Mistakes:
Confusing base with new instance creation
Thinking base deletes objects
Assuming base defines a class
2. Which of the following is the correct way to call a parent class constructor using base in C#?
easy
A. public Child() : base() { }
B. public Child() base() { }
C. public Child() call base() { }
D. public Child() : parent() { }
Solution
Step 1: Recall syntax for calling parent constructor
In C#, to call a parent constructor, use : base() after the child constructor signature.
Step 2: Match correct syntax
public Child() : base() { } uses public Child() : base() { }, which is the correct syntax.
Final Answer:
public Child() : base() { } -> Option A
Quick Check:
Parent constructor call = : base() [OK]
Hint: Use colon and base() after constructor name [OK]
Common Mistakes:
Omitting colon before base()
Using base() inside constructor body incorrectly
Using wrong keyword like parent()
3. What will be the output of the following code?
class Parent {
public virtual void Show() {
Console.WriteLine("Parent Show");
}
}
class Child : Parent {
public override void Show() {
base.Show();
Console.WriteLine("Child Show");
}
}
var obj = new Child();
obj.Show();
medium
A. Child Show
B. Parent Show
C. Compilation error
D. Parent Show\nChild Show
Solution
Step 1: Understand method overriding and base call
The Child class overrides Show() and calls base.Show() which runs the Parent version first.
Step 2: Trace the output
First, "Parent Show" is printed from base.Show(), then "Child Show" is printed from the child method.
class Parent {
public void Display() {
Console.WriteLine("Parent Display");
}
}
class Child : Parent {
public override void Display() {
base.Display();
Console.WriteLine("Child Display");
}
}
medium
A. Cannot override non-virtual method Display() in Parent.
B. base.Display() is invalid syntax.
C. Child class must not call base.Display().
D. No error, code runs fine.
Solution
Step 1: Check method overriding rules
In C#, only methods marked virtual or abstract in the parent can be overridden.
Step 2: Analyze the Parent class method
The Display() method in Parent is not virtual, so override in Child causes a compile error.
Final Answer:
Cannot override non-virtual method Display() in Parent. -> Option A
Quick Check:
Override requires virtual method [OK]
Hint: Only virtual methods can be overridden [OK]
Common Mistakes:
Trying to override non-virtual method
Confusing base call syntax
Assuming override works without virtual
5. Given the classes below, what will be the output?
class A {
public virtual string GetName() => "A";
}
class B : A {
public override string GetName() => "B";
}
class C : B {
public override string GetName() => base.GetName() + "C";
}
var obj = new C();
Console.WriteLine(obj.GetName());
hard
A. AC
B. Compilation error
C. BC
D. C
Solution
Step 1: Trace method calls through inheritance
Class C overrides GetName() and calls base.GetName(), which refers to B's override returning "B".
Step 2: Combine returned strings
C appends "C" to the result from B, so the final string is "B" + "C" = "BC".
Final Answer:
BC -> Option C
Quick Check:
base.GetName() calls immediate parent method [OK]
Hint: base calls immediate parent method, not grandparent [OK]