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 3970188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:03:23+00:00 2026-05-20T04:03:23+00:00

Hello from C# and OOP newbie. How can I avoid change of class on

  • 0

Hello from C# and OOP newbie.

How can I avoid change of class on assigning derived class object to base class object in c#?
After i run code bellow i get this response

obj1 is TestingField.Two
obj2 is TestingField.Two

I expected that i will lose access to derived methods and properties (which I did) after assigning reference but I did not expect change of class in midcode :S

using System;

namespace TestingField
{
    class Program
    {
        static void Main(string[] args)
        {
            One obj1 = new One();
            Two obj2 = new Two();
            obj1 = obj2;
            Console.WriteLine("obj1 is {0}", obj1.GetType());
            Console.WriteLine("obj2 is {0}", obj2.GetType());
            Console.ReadLine();
        }
    }

    class One
    {
    }

    class Two : One
    {
        public void DoSomething()
        {
            Console.WriteLine("Did Something.");
        }

    }
}
  • 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-20T04:03:23+00:00Added an answer on May 20, 2026 at 4:03 am

    While you are right, you will lose access to members declared in the derived type, the object won’t suddenly change it’s type or implementation. You can access only members declared on the base type, but the implementation of the derived type is used in the case of overriden members, which is the case with GetType, which is a compiler generated method which automatically overrides the base class’s implementation.

    Extending your example:

    class One
    {
       public virtual void SayHello()
       {
          Console.WriteLine("Hello from Base");
       }
    }
    
    class Two : One
    {
        public void DoSomething()
        {
            Console.WriteLine("Did Something.");
        }
        public override void SayHello()
        {
          Console.WriteLine("Hello from Derived");
        }
    
    }
    

    Given:

    One obj = new Two();
    obj.SayHello(); // will return "Hello from Derived"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i get Base's hello method from Up class in code below? class
Expecting Hello from the derived. but getting Hello from the base.. class Program {
After always running monolithic blocks of code from within my AppController object, I've just
Hello I was trying to remove objects from object array that I have and
Hello this is code snippet which i get from Jquery Ajax based search I
Hello i can't import the R with the layouts resources from my app this
Can someone explain me the difference between Class.forName() and Thread.currentThread().getContextClassLoader().loadClass(). I have next code
class A2 { int numt=111; void hello() { System.out.println(Hello from A2); } } class
Hello I'm working with Javascript OOP and I can't get it to work. I
I have this stadard code for receiving a class from a web service: function

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.