Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Override Method

This C# article shows how the override keyword works on virtual base methods.

Override affects virtual method usage.

Virtual methods are meant to be re-implemented in derived classes. The override keyword specifies that a method replaces its virtual base method.

Example. This program illustrates the difference between an override method in a derived class, and a method that is not an override method. It does nothing useful but helps us learn about override methods.

Here: In the example, the class A is the base class. It has the virtual method Y.


And: In class B, we override Y. In class C, we implement Y but do not specify that it overrides the base method.


C# program that uses override modifier

using System;

class A
    public virtual void Y()
	// Used when C is referenced through A.

class B : A
    public override void Y()
	// Used when B is referenced through A.

class C : A
    public void Y() // Can be "new public void Y()"
	// Not used when C is referenced through A.

class Program
    static void Main()
	// Reference B through A.
	A ab = new B();

	// Reference C through A.
	A ac = new C();



In this example, the A type is used to reference the B and C types. When the A type references a B instance, the Y override from B is used. But when the A type references a C instance, the Y method from the base class A is used.

Note: The override modifier was not used. The C.Y method is local to the C type.

Warning: In the above program, the C type generates a warning because C.Y hides A.Y. Your program is confusing and could be fixed.

Tip: If you want C.Y to really "hide" A.Y, you can use the new modifier, as in "new public void Y()" in the declaration.

New Modifier

Research. The C# specification helps us understand override methods. With override, we specialize an "existing inherited virtual method." We provide a new implementation for it. This is at first confusing.

Tip: I recommend writing a test program to see how this works. This may help you understand.

Whereas a virtual method introduces a new method, an override method specializes an existing inherited virtual method by providing a new implementation of that method.

The C# Programming Language

Summary. The override modifier is needed for implementing polymorphic behaviors in derived classes. You can re-implement a virtual base method. This causes the base implementation to be ignored in favor of the "override" method.

And: This polymorphic behavior is core to object-oriented design in many programs.

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf