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
CSUGDENAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If you don't mark it virtual, then it cannot be overridden.
1
CSUGDENAuthor Commented:
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.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Make the Equipment.deAllocate virtual.

virtual bool deAllocate();

FYI . . . there's also the sealed keyword to make sure something doesn't get inheritted.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

käµfm³d 👽Commented:
When I attempt to override it (for a check)...
Can you show how you did that?
0
CSUGDENAuthor Commented:
käµfm³d,

I  got the error as follows:

Namespace TEDAEquipment
{
     public class Adder : PTD
     {
        public bool deAllocate()
        {
            return false;
        }
    }
}
0
Kanti PrasadCommented:
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();
0
käµfm³d 👽Commented:
OK, that is what I arrived at as well. Technically speaking, what you've done isn't overriding. It's referred to as "method hiding," and as the warning you've received mentions you'd typically include the new modifier to indicate your intention to other developers.

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

Open in new window


AFAIK, there is nothing that you can do to prevent this. There is a technical difference between this and overriding a method, and I'll try to think of a simple way to explain it...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CSUGDENAuthor Commented:
So that means that the run-time can SEE the method but it cannot be overridden. Correct?
0
Gustav BrockCIOCommented:
> 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
0
käµfm³d 👽Commented:
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.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.