C# Programming - Inheritance - Discussion
Discussion Forum : Inheritance - General Questions (Q.No. 8)
8.
Which of the following is correct about the C#.NET snippet given below?
namespace IndiabixConsoleApplication
{
class Baseclass
{
public void fun()
{
Console.WriteLine("Hi" + " ");
}
public void fun(int i)
{
Console.Write("Hello" + " ");
}
}
class Derived: Baseclass
{
public void fun()
{
Console.Write("Bye" + " ");
}
}
class MyProgram
{
static void Main(string[ ] args)
{
Derived d;
d = new Derived();
d.fun();
d.fun(77);
}
}
}
Discussion:
5 comments Page 1 of 1.
Jana said:
1 decade ago
d.fun() will call the derived class fun() method.
d.fun(int) will call the fun(int) method which is in the base class as it is not existing in base class.
d.fun(int) will call the fun(int) method which is in the base class as it is not existing in base class.
(1)
Ncn said:
1 decade ago
d.fun() will call fun() of derived class and for d.fun(77) since no match is found in derived class complier will search for matching typecast in base class.
Thus it will find fun(int i) and will execute it.
Thus it will find fun(int i) and will execute it.
Tejas Vishwakarma said:
1 decade ago
Its produces a warning during override. It would be avoid by using override keyword.
New said:
1 decade ago
New keyword should be in the method func() of derived class to remove the warning.
Kazeem said:
1 decade ago
Whenever we are calling a Method the compiler First searches for the method in Derived class, if it doesnot finds it there. Then it searches it in base class.
Since fun() is available in Derived class. So it executes it. Rather than executing the base class fun().
Since fun() is available in Derived class. So it executes it. Rather than executing the base class fun().
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers