Click here to Skip to main content
15,074,942 members
Please Sign up or sign in to vote.
2.78/5 (2 votes)
See more:
Hello my friends. I've encountered this question in a interview. Here it is:

C#
public Class A{
  public void Print()
  {
     Console.WriteLine("Hello From A");
  }
}

public Class B : A{
  public void Print()
  {
     Console.WriteLine("Hello From B");
  }
}

public Class C : A{
  public void Print()
  {
     Console.WriteLine("Hello From C");
  }
}


Now in the Main(). I have the :
C#
A objA;


The question is : How to use objA to call the Print() in the class C ?
NOTE THAT : THE LINE "HELLO FROM C" MUST BE PRINTED!!
And can you explain it to me?

Any helps would be appreciate :_)
Posted
Updated 23-Mar-14 4:17am
v2
Comments
Raul Iloc 24-Mar-14 15:07pm
   
Did you see my 2nd solution (țSolution 3Ț)?

You have to create (or use) an object of type C;
C#
A objA = new C();
 objA.Print();


The A is the base class for C class and in C class you override the Print method, so when an object of type C is used, the method of C class will be used even the object was declared as type A (base class). Similar could be for the other sibling class: B.

You can find more detailed explanation and examples in the next MSDN link[^]

So as you can see from the explanation and examples provided at this link, you have to mark your Print method from the base class as virtual, then in the derived classes (B and C) with override .
   
v5
Comments
Benjamin Nguyễn Đạt 23-Mar-14 10:16am
   
Hi Raul,
When the objA.Print() is called, it produces the "Hello From A".
I want the line "Hello From C" to be produced instead. :).
Sergey Alexandrovich Kryukov 23-Mar-14 12:19pm
   
True. It will write "Hello From C" only if you mark Pring() as virtual (override in derived classes).
—SA
Raul Iloc 23-Mar-14 14:09pm
   
It seems, that you didn't have a look to the explanation and examples from the provided link.
See the update in my solution!
Benjamin Nguyễn Đạt 24-Mar-14 4:10am
   
Hi Raul! I know that what virtual and override is in OOP. I wonder if the interviewer was right or wrong because he did not provide these keyword in his question.
Raul Iloc 24-Mar-14 4:57am
   
Maybe he expected from you to correct also the given code with this missing keywords.
Benjamin Nguyễn Đạt 25-Mar-14 4:46am
   
Yeah! That's right! It's just my bad knowledge on the Inheritance and Polymorphism
Raul Iloc 24-Mar-14 5:05am
   
See also my new solution in the case you are supposing to nor modify the given classes in "Solution 3".
Sergey Alexandrovich Kryukov 23-Mar-14 12:30pm
   
Sorry, really sorry, but you failed again with OOP. But I hope this is not because you don't understand things (after previous arguments, I checked up some of your past answers — you've done many good ones), maybe only because you did not pay attention: the OP code has no virtual methods, hence the OOP mechanism fails to work. I answered on how to make is working.

Another little thing is terminology: this is not yet polymorphism, but would be late binding. We cannot talk about polymorphism based on just one object. "Poly" suggests "many", "diverse"...

—SA
Raul Iloc 23-Mar-14 14:01pm
   
The method in the base class must be virtual, and in the link provided by me this is clear!
In my solution I underlined by using code only how the object must be created, but it seems that you like to attack other people answers...
Raul Iloc 23-Mar-14 14:13pm
   
I didn't failed with OOP neither here and neither in the post where you attacked me first time, and I provided correct links from MSDN in both cases.
Sergey Alexandrovich Kryukov 23-Mar-14 20:14pm
   
I can see that you mentioned "virtual" and "override" in last version of your answer, so I re-voted to 5.

Did you change it after my comment, or it was there before, and I simply failed to see it?
And please dismiss my doubt: I have an impression that you down-voted my answer after I down-voted yours. I am asking because I observed the same thing last time. So, is that really so, or am I mistaken?

—SA
Raul Iloc 25-Mar-14 3:31am
   
I voted your solution with 5!
Sergey Alexandrovich Kryukov 25-Mar-14 11:28am
   
Great, thank you.
—SA
If the source code was given by your tester, and supposing that you are not allowed to modify the given classes, the solution is the next one:

C#
namespace ConsoleApplication1
{
    public class A
    {
        public void Print()
        {
            Console.WriteLine("Hello From A");
        }
    }

    public class B : A
    {
        public void Print()
        {
            Console.WriteLine("Hello From B");
        }
    }

    public class C : A
    {
        public void Print()
        {
            Console.WriteLine("Hello From C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A test = new C();
            ((C)test).Print();
        }
    }
}


So test object is of type C and it has two Print() method (one inherited from its base class), and if you want to access its own Print() method you must use explicit cast before to invoke it.
   
v4
Comments
   
That's correct, a 5. OP later clarified that C.Print should be called in case the method is not virtual, so, this is about method hiding. It really requires type case. I credited this answer in my comment to mine.
—SA
Raul Iloc 25-Mar-14 3:30am
   
Thank you for your feedback and vote!
Let's consider modified code:
sc
public Class A {
  public virtual void Print() { Console.WriteLine("Hello From A"); }
}

public Class C : A{
  public override void Print() { Console.WriteLine("Hello From C"); }
}

// ...

A someObject = new C();
someObject.Print();


Optionally, A.Print could be abstract. Then and only then you will get what you want.

This mechanism is the heart of OOP and is called late binding. This is not yet polymorphism, which takes place when you have a whole set of object known by their compile-time type of the base class, and runtime type are different (hence poly), then they can be processed on the common basis, only through the interface
provided by a base type of the set, without knowing of the runtime types.

For some explanation of this core mechanism of OOP, please see my past answer: I couldn't figure out this portion of C# code[^].

—SA
   
v3
Comments
Benjamin Nguyễn Đạt 24-Mar-14 4:06am
   
But the interviewer asked me exactly what i typed in the question. there was no virtual keyword in the base's method. Did he right or wrong when asked me this kind of question?
Sergey Alexandrovich Kryukov 24-Mar-14 10:59am
   
Not ifs not buts. Without virtual or abstract it cannot work. The question would make sense if the interviewer gave you not
A objA;
but
A objA = new C();
Then you could be able to call C.Print(), even non virtual. (Isn't it obvious? Can you do it?)

Without that, objA is null, so you could not call anything at all.

If objA = new C(), correct answer is Solution 3.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900