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

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s