Case 1
class baseclass
{
public void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
}
class Program
{
static void Main(string[] args)
{
childclass1 c = new childclass1();
c.childmethod1();
c.basemethod(); // is this possible? error, warning, nothing?
Console.ReadKey();
}
}
c.childmethod1() -- output would be "--Child Method1--"
c.basemethod(); -- No error, no warning, o/p would be "--base method--". Child class has access to its parent public and protected members.
----------------------------------------------------------------------------------------------------------------------------------------
Case 2
class baseclass
{
public virtual void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
}
class Program
{
static void Main(string[] args)
{
childclass1 c = new childclass1();
c.basemethod(); // is this possible? error, warning, nothing?
Console.ReadKey();
}
}
c.basemethod(); -- No error, no warning, o/p would be "--base method--" as nothing is override in child method. and code flow is from parent to child as waterfall. So parent method will be called.
----------------------------------------------------------------------------------------------------------------------------------------
Case 3:
class baseclass
{
public virtual void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
public new / <<space>> / virtual / override void basemethod()
{
Console.WriteLine("--base method from child--");
}
}
class Program
{
static void Main(string[] args)
{
childclass1 c = new childclass1();
c.basemethod(); // is this possible? error, warning, nothing?
Console.ReadKey();
}
}
c.basemethod(); -- No error, no warning, o/p would be "--base method from child--" as object is pure form of child class.it wont simply look at parent method.
----------------------------------------------------------------------------------------------------------------------------------------
Case 4:
class baseclass
{
public virtual void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
public new / <<space>> / virtual void basemethod()
{
Console.WriteLine("--base method from child--");
}
}
class Program
{
static void Main(string[] args)
{
baseclass b = new childclass1();
b.basemethod();
Console.ReadKey();
}
}
c.basemethod(); -- o/p would be "--base method--" now method will be from parent to child as waterfall. Since there is no override keyword. basemethod of parent will be called in above case.
Even if parent doent have virtual keyword then still only parent method would have called.
----------------------------------------------------------------------------------------------------------------------------------------
Case 5:
class baseclass
{
public virtual void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
public override void basemethod()
{
Console.WriteLine("--base method from child--");
}
}
class Program
{
static void Main(string[] args)
{
baseclass b = new childclass1();
b.basemethod();
Console.ReadKey();
}
}
c.basemethod(); -- o/p would be "--base method from child--" here method will be called from parent to child. since parent has virtual keyword and child has override keyword, method of child would be called.
----------------------------------------------------------------------------------------------------------------------------------------
Case 6:
class baseclass
{
public void basemethod()
{
Console.WriteLine("--base method--");
}
}
class childclass1 : baseclass
{
public override void childmethod1()
{
Console.WriteLine("--Child Method1--");
}
}
class Program
{
static void Main(string[] args)
{
childclass1 c = new childclass1();
c.basemethod(); // is this possible? error, warning, nothing?
Console.ReadKey();
}
}
Compile time error: Override can not exists without virtual/abstract keyword. While virtual can exists without override.
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment