Link to home
Start Free TrialLog in
Avatar of Charles Sugden
Charles SugdenFlag 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
Avatar of kaufmed
kaufmed
Flag of United States of America image

If you don't mark it virtual, then it cannot be overridden.
Avatar of 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.
Make the Equipment.deAllocate virtual.

virtual bool deAllocate();

FYI . . . there's also the sealed keyword to make sure something doesn't get inheritted.
When I attempt to override it (for a check)...
Can you show how you did that?
käµfm³d,

I  got the error as follows:

Namespace TEDAEquipment
{
     public class Adder : PTD
     {
        public bool deAllocate()
        {
            return false;
        }
    }
}
Avatar of Kanti Prasad
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();
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So that means that the run-time can SEE the method but it cannot be overridden. Correct?
> 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
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.