Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:51:32+00:00 2026-06-04T17:51:32+00:00

Given public class Animal { public Animal() { Console.WriteLine(Animal constructor called); } public virtual

  • 0

Given

public class Animal
{
    public Animal()
    {
        Console.WriteLine("Animal constructor called");
    }
    public virtual void Speak()
    {
        Console.WriteLine("animal speaks");
    }
}

public class Dog: Animal
{
    public Dog()
    {
        Console.WriteLine("Dog constructor called");
        this.Speak();
    }
    public override void Speak()
    {
        Console.WriteLine("dog speaks");
  base.Speak();
    }
}

this.Speak() calls Dog.Speak(). Remove Speak() from dog and suddenly this.Speak() calls Animal.Speak(). Why does this behave this way? In other words, why does this mean base or this?

To me, an explicit call to base.Speak() makes more sense. Especially when speak is not virtual, surprisingly Speak() is still called when virtual is removed. I understand IS-A relationships from an OO sense, but I can’t wrap my head around this specific problem in C#. This gets especially annoying when people write God class UI’s (practically every business does). I’m looking for “Speak()” inside “this” when I should be looking at “base”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:51:33+00:00Added an answer on June 4, 2026 at 5:51 pm

    Subclases automatically inherit behavior from their base classes. If you don’t do anything other than inherit Dog from Animal then this.Speak() and base.Speak() both reference the version of Speak() that was implemented in Animal.

    Where special things start happening is if Dog overrides Speak(). This is not possible unless Speak() is virtual. (The virtual keyword doesn’t control inheritance, it controlls overriding.)

    Only when Dog overrides Speak() does base.Speak() do something special: In that case, calling Speak() (or this.Speak()) will execute Dog‘s implementation, because it overrides Animal‘s implementation. This is where base becomes useful: it allows you to get around this behavior by specifying that you want to execute the base class’s implementation rather than the override.

    A common use of this style is in constructors. For example:

    public class Animal
    {
        private readonly string _name;
        public Animal() : this("Animal") { }
        protected Animal(string name) { _name = name; }
        public void Speak() { Console.WriteLine(_name + " speaks"); }
    }
    
    public class NamedAnimal : Animal
    {
        public NamedAnimal(name) : base(name) { }
    }
    
    // usage:
    (new Animal()).Speak();  // prints "Animal speaks"
    (new NamedAnimal("Dog")).Speak();     // prints "Dog speaks"
    

    In this example, NamedAnimal doesn’t have access to the _name field, but it is still able to set it indirectly by calling the base class’s constructor. But the base class’s signature is the same as one in the base class, so it has to be specified using base.

    With non-constructors it’s also useful to get at behavior that’s not otherwise accessible. For example, if Animal.Speak were virtual then we could use an override to tack behavior onto it rather than simply replacing it:

    public class NamedAnimal : Animal
    {
        public NamedAnimal(name) : base(name) { }
        public override Speak()
        {
            Console.Write("The animal named ");
            base.Speak();
        }
    }
    
    // usage:
    (new NamedAnimal("Dog")).Speak();  // Writes "The animal named Dog speaks"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Ex { public void eat(Animal animal){System.out.println(this is animal);} public void eat(Dog dog){System.out.println(this
Given this highly simplified example: abstract class Animal { } class Dog : Animal
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Given a base class with the following interface: public class Base { public virtual
Good Morning! Given: public class FooClass { public void FooMethod() { using (var myEntity
Given this class: public class OrderService { public OrderService(IOrderLogger log) { this.log = log;
Having difficulty finding relevant search results... Given this model: public abstract class A {
I have an Animal class and an extension of Animal called AnimalExtension. public class
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.