What base does
When a subclass overrides a method, the new body replaces the parent's body for objects of that subclass. Often you want to keep what the parent did and add something to it. Inside the subclass, the keyword base refers to the parent class, so base.Bark() runs the parent's version of Bark on the same object.
You have already seen base in constructors: : base(name) passes the name to the parent's constructor. base.Bark() is the same idea for an ordinary method.
Why extending beats copying
You could make Puppy print the Woof! line itself by typing the same Console.WriteLine again. That works until someone changes Dog.Bark: the puppy's copy would still print the old text. Calling base.Bark() keeps a single copy of the parent's behaviour, so every subclass that extends it follows the parent automatically.
The call also runs on the same object. That is why the parent's line shows the puppy's own name: base.Bark() does not create a Dog, it runs Dog's code with this still being the puppy.
The classes in this lesson
Dog has a public field name and a method Bark that prints Woof! I am followed by the name. Bark is marked virtual, which allows subclasses to override it. Puppy extends Dog and declares public override void Bark(), but the body is empty, so a puppy would print nothing at all.
Step by step
- Call the parent first. Inside
Puppy.Bark, writebase.Bark();. This runsDog's version, which prints theWoof!line using the puppy's ownname. - Add the puppy's own line. Print
"Yip! Said "followed byname. The field is inherited fromDog, soPuppycan use it directly.
The solution in C#
class Dog {
public string name;
public Dog(string name) { this.name = name; }
public virtual void Bark() {
Console.WriteLine("Woof! I am " + name);
}
}
class Puppy : Dog {
public Puppy(string name) : base(name) { }
public override void Bark() {
base.Bark();
Console.WriteLine("Yip! Said " + name);
}
}
class Solution {
public void MainFunction() {
Puppy buddy = new Puppy("Buddy");
buddy.Bark();
Puppy rex = new Puppy("Rex");
rex.Bark();
Dog dog = new Dog("Max");
dog.Bark();
}
}
Running it prints:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Each puppy prints two lines, parent line first. The plain Dog prints only the parent's line, because its own Bark was never changed.
Details worth knowing
virtualandoverridego together. A method can only be overridden if the parent marks itvirtual, and the subclass must sayoverride. Both are already written for you here.- The order is your choice. Putting
base.Bark()first gives the parent's line before the puppy's. If a puppy method printed its own line and then calledbase.Bark(), the output for a puppy named Zed would beYip! Said Zedand thenWoof! I am Zed. - Without the call, the parent's code does not run. If the override only printed the
Yip!line, a puppy named Zed would print justYip! Said Zed. Overriding replaces;baseis how you extend. - The override is used through a parent variable too. With
Dog d = new Puppy("Pip");, callingd.Bark()printsWoof! I am Pipand thenYip! Said Pip. The object decides which version runs, not the type of the variable.
How virtual and override choose a method
Marking Dog.Bark as virtual means the version that runs is picked when the program runs, from the real type of the object. An object created with new Puppy("Pip") is a Puppy even when it is stored in a Dog variable, so its Bark is the puppy's. An object created with new Dog("Max") is a plain Dog, so it gets the parent's version. In the driver there is no if anywhere: each object simply carries its own behaviour.
Changing the order
The two statements in Puppy.Bark are independent, so you can swap them. With the Yip! line first and base.Bark() after it, a puppy named Zed prints:
Yip! Said Zed
Woof! I am Zed
The lesson's tests expect the parent's line first, so keep base.Bark() above the puppy's own line.
What goes wrong when base or virtual is missing
If Dog.Bark were not marked virtual, the override would not compile. The compiler reports:
error CS0506: 'Puppy.Bark()': cannot override inherited member 'Dog.Bark()' because it is not marked virtual, abstract, or override
A related trap is writing Bark() instead of base.Bark() inside the override. That calls the puppy's own method again, which calls itself again, and the program ends in a stack overflow. The word base is what sends the call up to the parent.
Common mistakes
- Forgetting the parentheses and semicolon: the call is
base.Bark();. - Typing the greeting again instead of calling
base.Bark(). It would print the same text today, but it would stop matching the parent as soon as the parent changed. - Putting
base.Bark()after theYip!line. It compiles and runs, but the two lines come out in the wrong order for the lesson's expected output. - Using
basein a method the parent does not have.base.Bark()compiles only becauseDogdeclares an accessibleBark. - Hard-coding a name such as
"Buddy". Use thenamefield, so every puppy prints its own name.
Two uses of base
| Where | Form | What it does |
|---|---|---|
| After a constructor's parameters | : base(name) | Runs the parent's constructor with the given arguments, before the subclass constructor's body. |
| Inside an overriding method | base.Bark() | Runs the parent's version of that method on the current object. |
Testing
- Create
new Puppy("Pip")and callBark(): two lines,Woof! I am Pipand thenYip! Said Pip. - Create
new Dog("Max")and callBark(): one line,Woof! I am Max.
Key takeaways
base.Method()calls the parent's version of a method on the current object.- An override replaces the parent's behaviour unless it calls
base. - Where you place the
basecall decides whether the parent's work happens before or after the subclass's own.
