A
A
Alexey FRZ2018-09-12 17:28:59
.NET
Alexey FRZ, 2018-09-12 17:28:59

Redefining a method with override. Why doesn't it work?

Parent code:

public virtual int Perimeter()
        {
            return SideA + SideB + SideC;
        }

Successor code:
public override int Perimeter()
        {
            return SideA-SideB-SideC;
        }

Call:
Treangle obj = new Treangle();
Console.WriteLine(obj.Perimeter())

I create an object of type Triangle, i.e. parent. In the successor of Rectangular, I redefined it through override, but if I'm not mistaken, it overrides the method for the parent too. As a result, the code of the parent method is used, and not the one that is overridden. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2018-09-12
@leshqow

and after all if I am not mistaken overrides a method and for the parent too.

If an object is created as a parent, then the method will be the parent. Redefinition works only if the object was really created as a successor (and it doesn’t matter what data type it was later put into - the main thing is how it was constructed)

N
Namynnuz, 2018-09-13
@Naminnuz

This kind of inheritance leads to very fragile code. That is why OPP is not a simulation of reality, otherwise it will be impossible to maintain these drug addictions later, and any changes at any level will lead to the fact that somewhere something will constantly fall off and repairing in one place, we will break in another.
In addition to the fact that you should read at least some book on sharpei, you should also study the principles of SOLID.

N
Nikolai Bogdanov, 2018-09-13
@konoplinovich

The link is well written, here is a basic example about the difference between override and new modifiers :

using System;  
using System.Text;  

namespace OverrideAndNew  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            BaseClass bc = new BaseClass();  
            DerivedClass dc = new DerivedClass();  
            BaseClass bcdc = new DerivedClass();  

            // The following two calls do what you would expect. They call  
            // the methods that are defined in BaseClass.  
            bc.Method1();  
            bc.Method2();  
            // Output:  
            // Base - Method1  
            // Base - Method2  

            // The following two calls do what you would expect. They call  
            // the methods that are defined in DerivedClass.  
            dc.Method1();  
            dc.Method2();  
            // Output:  
            // Derived - Method1  
            // Derived - Method2  

            // The following two calls produce different results, depending   
            // on whether override (Method1) or new (Method2) is used.  
            bcdc.Method1();  
            bcdc.Method2();  
            // Output:  
            // Derived - Method1  
            // Base - Method2  
        }  
    }  

    class BaseClass  
    {  
        public virtual void Method1()  
        {  
            Console.WriteLine("Base - Method1");  
        }  

        public virtual void Method2()  
        {  
            Console.WriteLine("Base - Method2");  
        }  
    }  

    class DerivedClass : BaseClass  
    {  
        public override void Method1()  
        {  
            Console.WriteLine("Derived - Method1");  
        }  

        public new void Method2()  
        {  
            Console.WriteLine("Derived - Method2");  
        }  
    }  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question