General FAQs Part 2

Q:- What are Virtual and Hiding methods?

Ans:- Virtual Methods are used to be overridden in the derived classes. we cannot use the virtual modifier with the static, abstract, private, or override modifiers. Property can also be virtual except static properties. base keyword is used to call the base class overridden methods.
Hiding Methods are used to hide the base class method in the derived class.

class MyBaseClass
{
     public virtual string VirtualMethod()
     {
          return "This method is virtual and defined in MyBaseClass";
     }

     public string HideMethod()
     {
          return "This method is hidden and defined in MyBaseClass";
     }
}
class MyDerivedClass : MyBaseClass
{
     public override string VirtualMethod()
     {
          return "This method is override defined in MyDerivedClass";
     }

     public new string HideMethod()
     {
          return "This method is new defined in MyDerivedClass";
     }
}
public void Main()
{
     MyDerivedClass derivedClassObj = new myDerivedClass();
     Console.WriteLine(derivedClassObj.VirtualMethod());
     Console.WriteLine(derivedClassObj.HideMethod());
     MyBaseClass baseClassObj = derivedClassObj;
     Console.WriteLine(baseClassObj.VirtualMethod()); //calling derived class method
     Console.WriteLine(baseClassObj.HideMethod()); //calling base class method
}

Output: 
This method is override defined in MyDerivedClass
This method is new defined in MyDerivedClass
This method is override defined in MyDerivedClass
This method is hidden and defined in MyBaseClass

 

Q:- What are Sealed Classes and Methods?

Ans:- If class is declared as sealed, it means it can’t be inherit. In case of a method, this means that you can’t override the method.

sealed class FinalClass
{   // etc   }
class DerivedClass : FinalClass    //wrong. Will give compilation error
{    // etc  }

Declaring a method as a sealed servers a similar purpose as a class. In order to use the sealed keyword on method or property, it must be first overridden from a base class Otherwise don’t mark it as virtual in base class.

class MyClass : MyBaseClass
{
     public sealed override void FinalMethod()
     {  // etc  }
}
class DerivedClass : MyClass
{
     public override void FinalMethod()   //wrong. Will give compilation error
     { }
}
Advertisements

About Rajan
I am Rajan Garg working as a Senior Software Engineer in a company. I am working on the Microsoft technologies.

2 Responses to General FAQs Part 2

  1. Praveen Kumar Jayaram says:

    Good start Rajan πŸ™‚

    • Rajan says:

      Thanks buddy…you should also start one blog – Java Guru πŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: