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

  • Home
  • SEARCH
  • 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 6949799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:00:14+00:00 2026-05-27T14:00:14+00:00

class Dog { } class BullDog : Dog { } class Program { static

  • 0
class Dog
{
}
class BullDog : Dog
{
}
class Program
{
    static void Main()
    {
        Dog dog2 = new BullDog();
        BullDog dog3 = new BullDog();
    }
}
  1. What is the difference between using Dog as a reference vs BullDog as a reference?

  2. I have an habit of using var dog3 = new BullDog();, which is similar to BullDog dog2 = new BullDog();. When do we need to use Dog dog2 = new BullDog();?

  • 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-05-27T14:00:15+00:00Added an answer on May 27, 2026 at 2:00 pm

    EDIT: To address the additional question from the comments:

    static void TakesDog(Dog theDog) { ... }
    
    static void TakesBulldog(Bulldog theBulldog) { ... }
    
    static void TakesObject(object theObject) { ... }
    
    static void Main()
    {
        //Given these declarations...
        object dog = new BullDog();
        Dog dog2 = new BullDog();
        BullDog dog3 = new BullDog();
    
        //These calls will work because a BullDog is a Dog:
        TakesDog(dog2);
        TakesDog(dog3);
    
        //this call will work because a Bulldog is a Bulldog:
        TakesBulldog(dog3);
    
        //and these calls will ALL work because all Dogs are Objects:
        TakesObject(dog);
        TakesObject(dog2);
        TakesObject(dog3);
    
        //However, these calls will fail because an Object or Dog is not 
        //necessarily a Bulldog,
        //EVEN THOUGH our current dog and dog2 are indeed references to Bulldogs:
        TakesBulldog(dog);        
        TakesBulldog(dog2);
    
        //An explicit conversion is necessary to make the above calls work:
        TakesBulldog(dog2 as Bulldog); //works given the current reference
        TakesBulldog((Bulldog)dog2); //works given the current reference
        TakesBulldog(dog as Bulldog); //ditto
        TakesBulldog((Bulldog)dog); //ditto
    
        //but if we change the value of dog2 to some other dog:
        dog2 = new Labrador();
    
        //the above calls now fail:
        TakesBulldog(dog2 as Bulldog); //passes null into the method
        TakesBulldog((Bulldog)dog2); //throws InvalidCastException
    
        //you can avoid problems by checking the true type:
        if(dog2 is Bulldog) //interrogates the type of the referenced object
           TakesBulldog((Bulldog)dog2); //works
        else
           TakesDog(dog2); //general fallback case
    
        //Object is similar but even more basic:
        dog = "I'm a dog"; //now dog is a System.String
    
        //this call still works:
        TakesObject(dog);
    
        //but these will fail:
        TakesDog(dog);
        TakesBulldog(dog);
    }
    

    One last thing to understand:

    //given these declarations:
    object dog = new BullDog();
    BullDog dog2 = new BullDog();
    
    //even though dog is a BullDog, attempting to call BullDog-specific 
    //members (methods, properties, fields) will fail:
    dog.Drool();
    
    //you may only call members as specific as the object type of the 
    //variable holding the reference:
    dog.ToString(); //defined by Object. If you've overridden it in Dog or BullDog,
       //you'll get that implementation
    dog2.Drool(); //works because we know from the variable that dog2 is a BullDog.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, I can do this: class Program { static void Main(string[] args) {
Inheritance.java public class InheritanceExample { static public void main(String[] args){ Cat c = new
For example can I have class Dog { int legs; Bone chewy; Bone squeeky;
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Suppose I have a class Dog that inherits from a class Animal . What
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
I have a small program to demonstrate simple inheritance. I am defining a Dog
Let's suppose I have a class Dog that inherits from class Animal, you might
I have this model I'm showing in the admin page: class Dog(models.Model): bark_volume =
I have this Java class class Dog { private String name; public Dog() {

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.