9 Replies - 3194 Views - Last Post: 07 June 2011 - 12:57 PM Rate Topic: -----

#1 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Hiding an inherited abstract method

Posted 07 June 2011 - 05:56 AM

I have an abstract base class with a few method signatures defined. However, I want to change the signature in one of my derived classes. In my example below, I'm talking about the loadContent method specifically. Since it's abstract in the base class, I still have to define it in my derived class. However, is there any way for me to hide it so it can't be accessed?

Code example:

    abstract class spriteBase
    {
        protected Rectangle _size;
        public virtual Rectangle size
        {
            get
            {
                return size;
            }
            set
            {
                _size = value;
            }
        }

        protected float _scale = 1.0f;
        public virtual float scale
        {
            get
            {
                return _scale;
            }
            set
            {
                _scale = value;
            }
        }

        protected Vector2 _position;
        public virtual Vector2 position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }

        public abstract void loadContent(ContentManager cm);
        public abstract void draw(SpriteBatch sb);

    }
    class background:spriteBase
    {
        public void loadContent(ContentManager cm, string gfx)
        {
            //do stuff
        }
        public override void loadContent(ContentManager cm)
        {
            throw new NotImplementedException();
        }
        public override void draw(SpriteBatch sb)
        {
            //do stuff
        }
    }



If this were C++, I'd just make the inherited abstract method private and be done with it, but that's not legal in C#. Any recommendations on how to handle a situation like this?

Is This A Good Question/Topic? 0
  • +

Replies To: Hiding an inherited abstract method

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9072
  • View blog
  • Posts: 34,106
  • Joined: 12-June 08

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 06:38 AM

Simple - either delete it from the abstract class (or comment it out), or just don't use it in the derived class.

It makes total sense that you can't make a method in an abstract class private.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4461
  • View blog
  • Posts: 7,771
  • Joined: 08-June 10

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 06:39 AM

Well, not as such. You could mark it with the Obsolete attribute. If you set it's IsError property to true, it'll warn you in IntelliSense, mark it as an error in Visual Studio, and throw an exception if anyone calls it.

Other than that, I don't know how you can change the visibility of an abstract method you have to inherit.
Was This Post Helpful? 1
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 06:42 AM

Why is the method shaped the way it is in the abstract class? This is a sign that you didn't plan your abstract properly... and setting it private wouldn't make any sense, what point would a private abstract method serve? If you would just make it private, might as well just delete it, like modi suggests.
Was This Post Helpful? 1
  • +
  • -

#5 Robin19  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 07:30 AM

The standard XNA way to access LoadContent is with Protected. Then it is like private, except derived classes can also call it. I think that is what you want to use here.
Was This Post Helpful? 0
  • +
  • -

#6 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 07:44 AM

Thanks for all the feedback.

The reason I'm creating the loadContent method in the base class is that all of my derived classes must have the method. It's abstract, however, because how the derived classes implement it can vary tremendously. Most of my derived classes work fine with the abstract signature, but in one case I want to be able to pass in a string as well.

Looks like that's not really the way to do it in C#, though. Most of my official training has been in C++, and if you want to make a method "disappear" in a derived class, you can just make it private and effectively hide it. I was basically curious if there's a similar method in C#, but it sounds like there really isn't, not without getting messy.

I suppose as an alternative I could rewrite that 'weird' derived class with the gfx string as a property, so when I run the loadContent method it'll just check that property. That's fine too, just not quite as contained. Thanks again for the input all.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,634
  • Joined: 16-October 07

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 08:31 AM

// bad naming for C#, should be SpriteBase
abstract class spriteBase {
	protected Rectangle _size;
	// should be Size... while the hell is Rectangle a size?
	public virtual Rectangle size {
		// this is wrong, you just made an infinite loop
		get { return size; }
		set { _size = value; }
	}



View Postsatis, on 07 June 2011 - 08:56 AM, said:

If this were C++, I'd just make the inherited abstract method private and be done with it


Huh? No, that wouldn't work either. If I rely on polymorphism, you've just broken it.

It would be a poor design decision in C++, but the language allows getting around certain restrictions if you're explicit about it. C# simply doesn't allow you to break OO design.
Was This Post Helpful? 2
  • +
  • -

#8 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 12:10 PM

Thanks for the feedback baavgai. I realize my naming conventions go against standard practice in some instances... you can blame javascript. :P I didn't notice the infinite loop... you may have just saved me a bunch of time debugging, thanks. Regarding why rectangle is a size... I'm actually playing with an XNA tutorial. The rectangle is supposed to define the outside boundary of the sprite, though why they called it size instead of "boundary" or "outline" or something I don't know.

You also make a good point about C++. I was specifically thinking about the means of keeping copy constructors and such under control, but you're right, hiding a method like I was thinking doesn't make sense. I'll need to rethink my approach.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4461
  • View blog
  • Posts: 7,771
  • Joined: 08-June 10

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 12:29 PM

Baavgai's right about the property being an infinite loop. Or, more accurately, it's infinite recursion that'll end in a stack overflow exception. You need to return the protected (which should be private) field, _size.
Was This Post Helpful? 0
  • +
  • -

#10 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Hiding an inherited abstract method

Posted 07 June 2011 - 12:57 PM

Just to throw it out there and not about the design of application but use of ovveride, new, 'shadowing'

I saw you said you wanted to pass a string to one of them again just showing and not considering any type of good or bad design

It is all in the help files but here is some code to demonstrate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            Derived derived = new Derived();
            derived.Method();

            DerivedOverride derivedOverride = new DerivedOverride();
            derivedOverride.Method();

            DerivedNewHide derivedNewHide = new DerivedNewHide();
            derivedNewHide.Method();

            DerivedString derivedString = new DerivedString();
            derivedString.Method();

            DerivedString derivedStringAndParameter = new DerivedString();
            derivedStringAndParameter.Method("Using the parameter");

            Console.ReadKey();


        }
    }


    public abstract class Base
    {
    
        public virtual void Method()
        {
            Console.WriteLine("Base class");
        }

    }

    public class Derived: Base
    {

    }


    public class DerivedOverride : Base
    {

        
         public override void Method()
        {
            Console.WriteLine("DerivedOverride class");
        }

    }

    public class DerivedNewHide : Base
    {
      
        //Hides it
        new public void Method()
        {
            Console.WriteLine("DerivedNewHide class");
        }

    }

    public class DerivedString : Base
    {    
        // 'Shadows it
         public void Method(string parameter)
        {
            Console.WriteLine("DerivedString class " + parameter );
        } 

    }

}



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1