Virtual-Override and New
Class Child inherits from Class Parent and has mehtod ShowVirtualOverride() and ShowNew() methods.
Please see the code snippet below and answer questions below:
class Parent
{
public virtual void ShowVirtualOverride()
{
Console.WriteLine("ParentVirtual");
}
public virtual void ShowNew()
{
Console.WriteLine("ParentNew");
}
}
class Child : Parent
{
public override void ShowVirtualOverride()
{
Console.WriteLine("ChildVirtual");
}
public new void ShowNew()
{
Console.WriteLine("ChildNew");
}
}
Question 1:
What is output of below code :
Parent objA = new Parent();
objA.ShowVirtualOverride();
objA.ShowNew();
Answer 1:
Output is below :
ParentVirtual
ParentNew
Question 2:
What is output of below code :
Parent objB = new Clild();
objB.ShowVirtualOverride();
objB.ShowNew();
Answer 2:
Output is below :
ChildVirtual
ParentNew
Question 3:
What is output of below code :
Child objC = new Child();
objC.ShowVirtualOverride();
objC.ShowNew();
Answer 3:
Output is below :
ChildVirtual
ChildNew
Question 4:
What is output of below code :
Child objD = new Parent();
objD.ShowVirtualOverride();
objD.ShowNew();
Answer 4:
This gives compilation error
Popular Posts
-
Cache Callback provides an ability where by some actions can be performed when that item is removed from cache CacheItemRemovedCallback ex...
-
XML DOM loads the entire xml document into memory whereas Simple API for XML ( SAX ) does not load entire xml document into memory XML DOM ...
-
Different types of diagrams in UML : UseCase diagram Class diagram Object diagram State diagram Sequence diagram Collaboration diagram...
-
Virtual-Override and New Class Child inherits from Class Parent and has mehtod ShowVirtualOverride() and ShowNew() methods. Please see th...
-
Question : Name few C# 3.0 enhancements ? Answer : The C# 3.0 language enhancements are as follows: Implicitly typed local variables I...
-
If locaction attribute is specified, must be one of: Any, Client, Downstream, None, Server or ServerAndClient.
No comments:
Post a Comment