C # Inheritance Issues

I am creating an abstract base class that has functions implemented by other classes. My doubts are:

1) Do I need to give a "virtual" in front of every function that needs to be overridden by child classes? I see some examples without the virtual keyword, and yet they are open to redefinition.

2) I need to have a function that will be implemented in the base class, and I do not want it to be overridden by child classes. I added the "fixed" keyword for this function. The compiler error "member" cannot be sealed because it is not an override. Am I something wrong here?

abstract public class ShapeBase { private ShapeDetails _shapedDetails; public CampusCardBase(ShapeDetails shDetails) { _shapedDetails= shDetails; } public virtual void Draw(); public virtual float getWidth(); public virtual void Swap(); public virtual void Erase(); public sealed ShapeDetails getShapeDetails() { return _shapedDetails; } }; 
+6
source share
7 answers
  • For methods without implementations in an abstract class, use abstract :

     abstract public void Draw(); abstract public float getWidth(); abstract public void Swap(); abstract public void Erase(); 
  • Default methods are not overridden; they allow only derived classes to override if abstract , virtual or override declared (but not override sealed ).

    Therefore, you do not need to give getShapeDetails() any other modifiers than public :

     public ShapeDetails getShapeDetails() { return _shapedDetails; } 

On the side of the note, you must adhere to the .NET naming conventions and use method names using the Pascal name, so getWidth() becomes getWidth() and getShapeDetails() becomes getShapeDetails() .

In fact, you should use the property for your _shapedDetails field instead of the getShapeDetails() method:

 private ShapeDetails _shapedDetails; public ShapeDetails ShapedDetails { get { return _shapedDetails; } } 
+9
source

To be redefined, a member must be labeled both virtual and abstract. If abstract, the class must also be abstract, and the member has no implementation in the defining class. The virtual member must provide the implementation. Derived classes must override abstract members and can override virtual members. Non-virtual participants cannot be "sealed" because they cannot be redefined.

+1
source

point 1: instead of virtual you can make these functions abstract.

 public abstract void Draw(); public abstract float getWidth(); public abstract void Swap(); public abstract void Erase(); 

This means that this function MUST be overridden. Currently, you can subclass your abstract class without any definitions in it, and it will still compile. Saying these are abstract functions, they should be redefined. If you do not override, then the subclass will also be abstract.

And about paragraph 2: can you add a code that uses a fixed word?

+1
source

1- Link to override (link to C #)

You cannot override a non-virtual or static method. An overridden base method must be virtual, abstract, or overriding.


2- Link to with print (link to C #)

You can also use a sealed modifier by a method or property that overrides the virtual method or property in the base class. This allows you to allow classes derived from your class and prevent them from overriding specific virtual methods or properties.

+1
source

If it is an abstract method, the default method is virtual and must be implemented in the inheriting class. Otherwise, if this is not abstract, you need to have the virtual keyword. If you do not use a compile-time type method,

0
source

Only a virtual method can be overridden, and only an abstract method can be implemented (for example, interface methods). Otherwise, the only one who can do this is to declare a new method to "hide" the old one:

 new public void getShapeDetails() { // Hides base.getShapeDetails(); } 

Here is an example / explanation of the code.

 //----------------------------------------------------------------------------- // <copyright file="Program.cs" company="DCOM Productions"> // Copyright (c) DCOM Productions. All rights reserved. // </copyright> //----------------------------------------------------------------------------- namespace AbstractClassExample { using System; using System.Collections.Generic; using System.Linq; using System.Text; class Program { /// <summary> /// Example abstract base class /// </summary> public abstract class FooClass { /// <summary> /// Abstract Method /// </summary> public abstract void abstractFoo(); /// <summary> /// Virtual Method /// </summary> public virtual void virtualFoo() { // Todo } /// <summary> /// Normal Method /// </summary> public void Foo() { // Todo } } public class FooDeriver : FooClass { /// <summary> /// Implements base.abstractFoo /// </summary> public override void abstractFoo() { throw new NotImplementedException(); } /// <summary> /// Overrides base.virtualFoo /// </summary> public override void virtualFoo() { base.virtualFoo(); } /// <summary> /// Compiler ErrorError 1 /// cannot override inherited member 'ConsoleApplication1.Program.FooClass.Foo()' because it is not marked virtual, abstract, or override /// </summary> public override void Foo() { throw new NotImplementedException(); } } static void Main(string[] args) { // Program code } } } 
0
source
  • You can tag a function with virtual keywork if you want to provide a default implementation in a base class that can be overridden in derived classes; or you can mark it as abstract by delegating the implementation to derived classes.
  • If you want the function to NOT be overridden in child implementations, DO NOT mark it as virtual or abstract : just define the function as usual.
0
source

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


All Articles