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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:00:06+00:00 2026-06-01T14:00:06+00:00

Class—A class ClassA { public string c1() { return Class-A; } } Class—B class

  • 0

Class—A

class ClassA
{
    public string c1()
    {
        return "Class-A";
    }

}

Class—B

class ClassB:ClassA
{
    public string c2()
    {
        return "Class-B";
    }
}

Main Class

—–Part 1——————

 ClassA obj1 = new ClassB();
 string a = obj1.c1();//Here i will get only c1
 Console.WriteLine(a);
 Console.ReadLine();

—–Part 2——————

 ClassB obj1 = new ClassA();
 string a = obj1.c2();//Her i will get both c1 and c2
 Console.WriteLine(a);
 Console.ReadLine();

In Part-1,i will get only c1.I need to know whether variable (obj) is created for ClassA in stack and assign address of ClassB from Heap.What is actually happening?

In Part -2 ,Getting(compilation error) conversion error.What is actually happening behind the screen while executing this code.

Thanks,
Joby Kurian

  • 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-01T14:00:09+00:00Added an answer on June 1, 2026 at 2:00 pm

    Part 1

    ClassA obj1 = new ClassB();
    

    This creates an instance of ClassB – an object.

    It also declares a variable called obj1, of type ClassA. That variable’s value will always be a reference – either null or a reference to an instance of ClassA. In this case, the initial value is a reference to the newly created ClassB object. (A reference to a ClassB object can be used as a ClassA reference too, because ClassB derives from ClassA.)

    When you call c1, it just calls the implementation in ClassA – but within that method, if you printed out this.GetType() it would still return ClassB‘s type, as it’s operating “within” a ClassB object.

    I strongly suggest you don’t worry about the stack and the heap yet. Focus on the three different concepts:

    • Objects
    • References (ways of navigating to objects, or null)
    • Variables (named storage locations)

    Eric Lippert has blogged a lot about this sort of thing. You might want to start with The Stack Is An Implementation Detail.

    Part 2

    ClassB obj1 = new ClassA();
    

    This creates an instance of ClassA, and tries to assign a reference to the new object to a variable of type ClassB. That doesn’t work, because ClassA does not derive from ClassB. You can’t use a reference of compile-time type ClassA to a variable of type ClassB. As a demonstration of why this wouldn’t work, it’s a bit like doing this:

    string x = new object();
    Console.WriteLine(x.Length); // What could this possibly print out?
    

    Basically, inheritance doesn’t work that way round – you can treat an instance of ClassB as an instance of ClassA (in most cases) but you can’t treat an instance of ClassA as an instance of ClassB. You’d normally be adding more state (fields) in ClassB, so that information just wouldn’t be present in the instance of ClassA.

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

Sidebar

Related Questions

class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
class ZiggyTest{ public static void main(String[] args) { String str = aaaaaaaaabb; String []
class Program : CriticalFinalizerObject { static void Main(string[] args) { Program p = new
{ class Program { static void Main(string[] args) { int id = 0, stock
class MyClass<T> where T: new() { public Test() { var t = new T();
class Program { static void Main(string[] args) { Console.WriteLine(Fib 1: ); Console.ReadLine(); } long
class Foo() { public Bar [] bars; } class Bar () { public string
class Size{ Size(){ } //public final Size SMALL = new Size();//infinite loop final Size
class example: def exampleMethod(self): aVar = 'some string' return aVar In this example, how
class A { public override int GetHashCode() { return 1; } } class B

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.