Skip to content

Method overriding – C Sharp example

Method overriding – C Sharp example

Method overriding C# examples using base and child class relationship, interface and abstract class with explanation.

We’ll learn how a child class can use base class behaviors / methods and override some of them in child class if don’t want to use base class methods without removing the methods from base class.

[Method Override definition– Writing the method in child class with same name, signature and return type, which is also in base class is known as method overriding.
The child class can write its definition different that base class same method.]

Let’s understand method overriding in C# by example…

Let’s say a son is using his father’s home and car . See the below program (No overriding concept) that implements this scenario. After that we will see the method overridden code.

The output of this C# program will be as below

Output:

Father’s Home
Father’s Car

 //base class
class Father {
 
	public void home() {
		Console.WriteLine("Father's Home");
	}
 
	public void car() {
		Console.WriteLine("Father's Car");
	}
}
 
// child class
class Son : Father {
 
	
}
 
/*
 * Test
 */
public class TestOverriding {
 
	public static void Main(String[] args) {
 
		Father s = new Son();
		s.home();
		s.car();
		
	}
 
}

C# code using method overriding

At later point of time, Son no longer wants to use his father’s Car. But, he still wants to use father’s home.

As a solution, Son will override Car() method and implement for own. So, the Car method with same name will be written in Son class and implemented. This is in fact method overriding concept.

Now the output will be as below. Notice here Son’s Car is called. In previous example it was Father’s Car.

Output:

Father’s Home
Son’s Car

 /*
 * method overriding java example
 */
//base class
class Father {
 
	public void home() {
		Console.WriteLine("Father's Home");
	}
 
	public virtual void car() {
		Console.WriteLine("Father's Car");
	}
}
 
// child class
class Son : Father {
	
	public override void car() {
		Console.WriteLine("Son's Car");
	}
 
	public void mobile(){
		Console.WriteLine("Son's mobile");
	}
}
/*
 * Test method overriding
 */
public class TestOverriding {
 
	public static void Main(String[] args) {
 
	Father s = new Son();
            s.home();
            s.car();;	
    }
 
}

DOUBT ON METHOD OVERRIDING

Doubt arises in mind that if we are going to write the same method and implement in child class, then why don’t we delete that method from the base class as we will no longer use that method from parent class.

Lets think of above program example, some later point of time, Son wanted to use his own Car and not his Father’s car. So, he overridden the Car method. Now answer yourself- Should the Son destroy his Father’s Car?

Off course not, some other child might be using that.

Similarly, You might use some third party base class, where you may not allow to change the class. Secondly, the parent class methods may be being used by other classes in the project/ applications that will not work.

So, It is not a good idea to remove method from a base class when you override that in child a class.


Published inC# Programming Examples