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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:29:30+00:00 2026-05-16T17:29:30+00:00

I have two classes public class A { public A() { } } public

  • 0

I have two classes

 public class A
    {
        public A()
        {

        }
    }

    public class B:A
    {
        public B()
        {

        }
    }

and it the code in Main is as follows

    A oa = new B();
    B ob = new A();

Here line 1 compiles successfully while line 2 displays typecasting error. Why this happens. What exactly happens when new B() and new A() gets called?

  • 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-16T17:29:31+00:00Added an answer on May 16, 2026 at 5:29 pm

    What exactly happens when new B() and
    new A() gets called?

    • new A() constructs an object of type A on the heap and returns a reference to it.

    • new B() constructs an object of type B on the heap and returns a reference to it.

    Here line 1 compiles successfully
    while line 2 displays typecasting
    error. Why this happens.

    Since B subclasses A, it is valid for a reference of type A to refer to an object of run-time type B. After all, B is simply a “special case” of A.

    However, the converse is not true, because not all As can be considered Bs.
    Although this is strictly enforced by C#’s safe type-system even if there is no “real” incompatibility, the reasons for such restrictions are natural. Imagine, for example, that B declared a property public int Foo {get; set;}.
    How would you expect this to behave:

    B ob = new A();
    ob.Foo = 5;
    

    This is clearly illogical: the real object that the reference is referring to has no such property. Consequently, the compiler prohibits such constructs.

    Now imagine you changed your code to:

    B b = (B)new A();

    Here, you are telling the compiler that the object created, will, at run-time, be assignable to a reference of type B. This will compile fine, but since the assertion is clearly incorrect, a run-time InvalidCastException will be thrown.

    To summarize, C#’s type system (if you ignore dynamic and a few special cases) is both static and safe: you will not successfully be able to treat a concrete instance of A as though it were of type B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my code is like this: i have two classes first class: public class Box<E>
I have two classes Address and Employee as follows: public class Address { public
I have two classes: public class MultilingualString { public int Id { get; set;
I have a problem with hibernate and criterias. I have two Classes: public class
I have these two classes public class Person { } public class Company {
Let's say we have these two classes: public class Base { public static int
greetngs, i have two classes: [Serializable] public class FireGridUnit { public GridUnit FireGridLocation {
I have the following two classes: public class Address { public string AddressLine1 {
Supposed I have two classes like this: public class Parent { public string Id
I have two classes as below. public class Destination { public Destination() { _StringCollection

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.