Preventing C # subclass from overwriting a method

Say I have an abstract parent class called "Parent" that implements the "DisplayTitle" method. I want this method to be the same for every subclass that inherits "parent." I would like to get a compilation error if the subclass tries to implement its own "DisplayTitle" method. How to do it in C #. I believe in Java, I would just mark the method as "final", but I cannot find an alternative in C #. I fiddled with β€œsealed” and β€œcanceled,” but I cannot get the behavior I'm looking for.

For example, in this code:

using System; namespace ConsoleApplication1 { class Parent { public void DisplayTitle() { Console.WriteLine("Parent Title"); } } class ChildSubclass : Parent { public void DisplayTitle() { Console.WriteLine("Child Own Implementation of Title"); } static void Main(string[] args) { ChildSubclass myChild = new ChildSubclass(); myChild.DisplayTitle(); Console.ReadLine(); } } } 

I want to get a compilation error saying that "ChildSubClass" cannot override "DisplayTitle". I am currently getting a warning, but it looks like this is what I have to do, and I don't know the right attributes to indicate the method.

+6
source share
4 answers

How to do it in C #. I believe in Java, I would just mark the method as "final", but I cannot find an alternative in C #.

The rough equivalent is sealed in C #, but you usually only need a virtual method, and your DisplayTitle method DisplayTitle not virtual.

It is important to note that ChildSubclass does not override the DisplayTitle - it hides it. Any code that uses references only to Parent will not invoke this implementation.

Note that with as-is code, you should get a compilation warning that recommends adding the new modifier to the method in ChildSubclass :

 public new void DisplayTitle() { ... } 

You cannot stop derived classes from hiding existing methods, except to seal the class itself to prevent the creation of the entire derived class ... but there will be no callers who do not use the derived type directly.

What is your real problem here? Accidental abuse or intentional problems?

EDIT: Note that the warning for your sample code will look something like this:

 Test.cs(12,19): warning CS0108: 'ConsoleApplication1.ChildSubclass.DisplayTitle()' hides inherited member 'ConsoleApplication1.Parent.DisplayTitle()'. Use the new keyword if hiding was intended. 

I suggest you turn warnings into errors, and then it's harder to ignore them :)

+10
source

A derived class cannot override your method; you have not declared it virtual. Please note that in C # this is completely different from Java, all methods are virtual in Java. In C #, you must explicitly use the keyword.

A derived class can hide your method using the same name. This is probably the compilation warning you are talking about. Using a new keyword suppresses the warning. This method in no way cancels your original method, your base class code always calls the original method, not the one in the derived class.

+2
source

Use a modified modifier to prevent overriding classes, properties, or methods of subclasses. What doesn't work when you use print?

http://msdn.microsoft.com/en-us/library/88c54tsw.aspx

+1
source

I am sure that what you want is not possible in C # using keywords for a method modifier.

Sealed only applies when overriding a virtual method in an ancestor class, which then prevents further overrides.

0
source

Source: https://habr.com/ru/post/926973/


All Articles