Click here to Skip to main content
15,867,308 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 .
 
Share this answer
 
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.
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.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 25-Mar-14 3:10am    
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
 
Share this answer
 
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