Interviews5 answers
Can we call a base class method without creating instance?
5 Answers
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/* Output Name: John L. Malgraine SSN: 444-55-6666 Employee ID: ABC567EFG */
abhijeet ranaPosted Jan 9, 2018, 3:12 AM EST
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/* Output Name: John L. Malgraine SSN: 444-55-6666 Employee ID: ABC567EFG */
abhijeet ranaPosted Jan 9, 2018, 3:12 AM EST
By using static
Mukesh KumarPosted Aug 31, 2017, 5:47 AM EDT
can you give an example please.
Naveen BishtPosted Feb 16, 2017, 1:55 AM EST
It is possible if it’s a static method.
It is possible by inheriting from that class also.It is possible from derived classes using base keyword.
Samir BhogaytaPosted Jun 25, 2016, 12:11 AM EDT


