Avatar of Charles Sugden
Charles Sugden
Flag for United States of America asked on

How to prevent derived class from overriding a method

Good day!

I am still somewhat green to some OOP concepts so bear with my request:
I would like to create a base class that has a private method(audit) that is called from a deallocation method which I do not want to be derived/overridden. I have included the code and would like some advice on how to do this.
test.txt
C#.NET Programming

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

If you don't mark it virtual, then it cannot be overridden.
Charles Sugden

ASKER
When I attempt to override it (for a check) I get the message
Warning      1      'Equipment.Adder.deAllocate()' hides inherited member 'Equipment.Harness.deAllocate()'. Use the new keyword if hiding was intended.
Kyle Abrahams

Make the Equipment.deAllocate virtual.

virtual bool deAllocate();

FYI . . . there's also the sealed keyword to make sure something doesn't get inheritted.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
kaufmed

When I attempt to override it (for a check)...
Can you show how you did that?
Charles Sugden

ASKER
käµfm³d,

I  got the error as follows:

Namespace TEDAEquipment
{
     public class Adder : PTD
     {
        public bool deAllocate()
        {
            return false;
        }
    }
}
Kanti Prasad

Hi

http://rbwhitaker.wikidot.com/c-sharp-abstract-classes

virtual will give the derived classes the power to provide its own implementation with the word override . This is also the proof that polymorphism happens. So don't mark the method virtual so that it won't be overridden.

e.g  the word abstract ensures that you cannot create a new Player class like
        Player player = new Player();. but you can use it as
       Player player = new HumanPlayer();
________________________________________________________________look for virtual
abstract class Player
{
    public virtual MoveDirection MakeMove();
}
__________________________________________________________________look for override
namespace Inheritance
{
    class HumanPlayer : Player
    {
        public HumanPlayer()
        {
        }
 
        public override MoveDirection MakeMove()
        {
           
        }
    }
}

________________If you still want to access the base method look for base.
 MoveDirection moveDirection = base.MakeMove();
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Charles Sugden

ASKER
So that means that the run-time can SEE the method but it cannot be overridden. Correct?
Gustav Brock

> When I attempt to override it (for a check) I get the message ... use the new keyword ...

But why would you proceed? If you don't want to override, then respect the warning and don't continue.

/gustav
kaufmed

So that means that the run-time can SEE the method but it cannot be overridden. Correct?
Yes.

It would be a rare case for you to want to use the new modifier. Even though it appears that you are inheriting the method when you use it, in reality you are not. So this breaks inheritance. It could also cause issues if you were coding against the abstraction (i.e. the base class). Take for example:

using System;

namespace ConsoleApplication68
{
    class Program
    {
        static void Main(string[] args)
        {
            Test.Harness instance1 = new Test.Harness();
            Test.Harness instance2 = new Test.Adder();

            instance1.deAllocate();
            instance2.deAllocate();

            Console.ReadKey();
        }
    }

    namespace Test
    {
        public class Adder : Harness
        {
            public new bool deAllocate()
            {
                Console.WriteLine("In Adder.deAllocate");
                return true;
            }
        }

        public class Harness : Equipment
        {
            private int _eid;

            public int eid { get { return _eid; } set { _eid = 1; } }

            public virtual int Allocate()
            {
                return 1;
            }

            public virtual bool deAllocate()
            {
                Console.WriteLine("In Harness.deAllocate");

                // Update database
                Audit();
                return true;
            }

            private void Audit()
            {
                // Update database
            }
        }

        interface Equipment
        {
            int eid { get; set; }

            int Allocate();
            bool deAllocate();

        }
    }
}

Open in new window


You might expect that you would see the message "In Adder.deAllocate" because I have a deAllocate method defined. However, because I used the new modifier, the runtime does not use the implementation that is on Adder. I have hidden it. The base class cannot "see" the implementation of deAllocate that is defined on Adder because it is not considered to be an override of the base class' method.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy