Answer the question
In order to leave comments, you need to log in
Redefining a method with override. Why doesn't it work?
Parent code:
public virtual int Perimeter()
{
return SideA + SideB + SideC;
}
public override int Perimeter()
{
return SideA-SideB-SideC;
}
Treangle obj = new Treangle();
Console.WriteLine(obj.Perimeter())
Answer the question
In order to leave comments, you need to log in
and after all if I am not mistaken overrides a method and for the parent too.
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.
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 questionAsk a Question
731 491 924 answers to any question