ASP.NET

C#.NET

Posted on

Overriding methods:

In method overriding we can override a method in base class by creating similar method in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.

If we want to override base class method then we need to declare base class method with “virtual” keyword and the method which we created in derived class to override base class need to declare with “override” keyword like as shown below :

 

using System;

namespace Demo

{

class SampleA

{

public virtual void Show()

{

Console.WriteLine(“Sample A Test Method”);

}}

class SampleB:SampleA

{

public override void Show()

{

Console.WriteLine(“Sample B Test Method”);

}}

class Program

{

static void Main(string[] args)

{

SampleA a=new SampleA();

SampleB b=new SampleB();

a.Show();

b.Show();

a = new SampleB();

a.Show();

Console.ReadLine();}}}

 

Output:

 

Sample A Test Method

Sample B Test Method

Sample B Test Method

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements