This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Saturday, March 4, 2023

Method Overriding in c#

 Method Overriding in c#

Method overriding means multiple methods having same name and same signature.

Overriding can be achieved only in child parent class and not in same class.


Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding. 


In simple words, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the ways by which C# achieve Run Time Polymorphism(Dynamic Polymorphism).



1. Virtual : 

If method in base class declared by virtual keywork then it tells the compiler that this 

method can be overriden by its child class.

2. Override :

 If we want to override method of base class in child class it need to declare with 

override keyword. Here we can implement the method diff than base class method.

3. Base : 

In case of overriding if we want to access base class method in derived class then we can 

access it using base keyword



Example of method overloading:


class base_class

{

    public void gfg();

}


class derived_class : base_class

{

    public void gfg();

}


class Main_Method

{

 static void Main()

 {

    derived_class d = new derived_class();

    d.gfg();

 }

}


No comments:

Post a Comment