Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Explicit abstract methods.

Explicit abstract methods.

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krisp
    wrote on last edited by
    #1

    Just wondering, Why, in a child class, can you not set an abstract method you override, to call a different method that returns something else, but has the same signature. And declare the abstract method by Explicit means. I know it is a compile error, but is it not something they could have easily allowed, or are there behind the scenes issues im not understanding? You can do this with interfaces, so why not abstract methods? public class BaseClass { protected abstract string MyMethod(); } public class ChildClass : BaseClass { private int _MyInt = 6; protected override string BaseClass.MyMethod() { return _MyInt.ToString(); } public int MyMethod() { return _MyInt; } }

    K 1 Reply Last reply
    0
    • K krisp

      Just wondering, Why, in a child class, can you not set an abstract method you override, to call a different method that returns something else, but has the same signature. And declare the abstract method by Explicit means. I know it is a compile error, but is it not something they could have easily allowed, or are there behind the scenes issues im not understanding? You can do this with interfaces, so why not abstract methods? public class BaseClass { protected abstract string MyMethod(); } public class ChildClass : BaseClass { private int _MyInt = 6; protected override string BaseClass.MyMethod() { return _MyInt.ToString(); } public int MyMethod() { return _MyInt; } }

      K Offline
      K Offline
      Kentamanos
      wrote on last edited by
      #2

      Few comments... Classes with abstract methods have to be marked abstract. Think about it. You can't instantiate one because it doesn't know how to implement the abstract method, so the class becomes abstract. You can never define two functions with the same name that take the same parameters but return different types. This class would never compile (has nothing to do with abstract classes):

      public class test
        {
          string Fred()
          {
            return "one";
          }

      int Fred()
          {
            return 1;
          }
        }

      You probably know, but the second class should look like this:

      public class ChildClass : BaseClass
        {
          private int _MyInt = 6;

      protected override string MyMethod()
          {
            return _MyInt.ToString();
          }
        }

      Is there something else I'm missing on this?


      I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
      -David St. Hubbins

      K 1 Reply Last reply
      0
      • K Kentamanos

        Few comments... Classes with abstract methods have to be marked abstract. Think about it. You can't instantiate one because it doesn't know how to implement the abstract method, so the class becomes abstract. You can never define two functions with the same name that take the same parameters but return different types. This class would never compile (has nothing to do with abstract classes):

        public class test
          {
            string Fred()
            {
              return "one";
            }

        int Fred()
            {
              return 1;
            }
          }

        You probably know, but the second class should look like this:

        public class ChildClass : BaseClass
          {
            private int _MyInt = 6;

        protected override string MyMethod()
            {
              return _MyInt.ToString();
            }
          }

        Is there something else I'm missing on this?


        I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
        -David St. Hubbins

        K Offline
        K Offline
        krisp
        wrote on last edited by
        #3

        I was getting at the fact that you can do this with interfaces, so why not abstract classes? Seems like it should be logical, an abstract class is just like a regular class with the abstract methods comming from an interface, except it has the abstract keyword. public interface BaseInterface { string MyMethod(); } public class ChildClass : BaseInterface { private int _MyInt = 6; string BaseInterface.MyMethod() { return MyMethod().ToString(); // this calls int MyMethod() } public int MyMethod() { return _MyInt; } }

        K 1 Reply Last reply
        0
        • K krisp

          I was getting at the fact that you can do this with interfaces, so why not abstract classes? Seems like it should be logical, an abstract class is just like a regular class with the abstract methods comming from an interface, except it has the abstract keyword. public interface BaseInterface { string MyMethod(); } public class ChildClass : BaseInterface { private int _MyInt = 6; string BaseInterface.MyMethod() { return MyMethod().ToString(); // this calls int MyMethod() } public int MyMethod() { return _MyInt; } }

          K Offline
          K Offline
          Kentamanos
          wrote on last edited by
          #4

          I see what you're saying now. In order for you to call the ChildClass's "version" of the method (not the one that he implements to fulfill his derivation from the abstract class), you'd have to have some sort of syntax for specifying which version you were calling. In the case of the interfaces, it's really a function of casting to the inteface and calling the method. It doesn't make sense to cast an instance of the the derived class to the derived class to get the derived one's "personal" version. I'm sure this syntactical stuff (which is fairly nasty) was purposely avoided. That's one good reason to not allow multiple inheritance (which is a design descision MS made with C#).


          I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
          -David St. Hubbins

          K 1 Reply Last reply
          0
          • K Kentamanos

            I see what you're saying now. In order for you to call the ChildClass's "version" of the method (not the one that he implements to fulfill his derivation from the abstract class), you'd have to have some sort of syntax for specifying which version you were calling. In the case of the interfaces, it's really a function of casting to the inteface and calling the method. It doesn't make sense to cast an instance of the the derived class to the derived class to get the derived one's "personal" version. I'm sure this syntactical stuff (which is fairly nasty) was purposely avoided. That's one good reason to not allow multiple inheritance (which is a design descision MS made with C#).


            I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
            -David St. Hubbins

            K Offline
            K Offline
            krisp
            wrote on last edited by
            #5

            Ok, well this is pretty much exactly what i want to do with an abstract method. public class DataBase { ... } public class BaseClass { protected abstract DataBase DoMethod(); } public class DataChild : DataBase { ... } public class ChildClass : BaseClass { private int _MyInt = 6; protected override DataBase BaseClass.DoMethod() { return DoMethod(); } public DataChild DoMethod() { return new DataChild(); } } I am returning an object in my child's method that derives from the object being returned in my base's method. So, this would not have any problems that I can see. I don't see how this has anything to do with multiple inheritance, because I would still only be inheriting from one class. There is no reason why the return value can't be derived from the abstract method's return type. I can not see in any way this conflicting with any multiple inheritance, or anything. That way when the object is casted to its base class, the proper return type is still received. What am I missing for this not to work?

            K 1 Reply Last reply
            0
            • K krisp

              Ok, well this is pretty much exactly what i want to do with an abstract method. public class DataBase { ... } public class BaseClass { protected abstract DataBase DoMethod(); } public class DataChild : DataBase { ... } public class ChildClass : BaseClass { private int _MyInt = 6; protected override DataBase BaseClass.DoMethod() { return DoMethod(); } public DataChild DoMethod() { return new DataChild(); } } I am returning an object in my child's method that derives from the object being returned in my base's method. So, this would not have any problems that I can see. I don't see how this has anything to do with multiple inheritance, because I would still only be inheriting from one class. There is no reason why the return value can't be derived from the abstract method's return type. I can not see in any way this conflicting with any multiple inheritance, or anything. That way when the object is casted to its base class, the proper return type is still received. What am I missing for this not to work?

              K Offline
              K Offline
              Kentamanos
              wrote on last edited by
              #6

              I only mention multiple inheritance because having syntax to specify exactly whose version of a function you're calling is something that you HAVE to have once your language implements multiple inheritance. There's many an interview question on C++ based upon this type of crap ;). If you only have single inheritance, you can call your parent's implementation with a keyword like "base". One thing to realize is it doesn't matter if the methods return different types at all. Two functions can't have the same name and take the same parameters unless we can use a specific "vtable" to call each one. That said, think about this:

              namespace Test
              {

              public interface I
              {
              	void MethodA();
              }
              
              public class C : I
              {
              	void Test.I.MethodA()
              	{
              		// I's version
              	}
              
              	public void MethodA()
              	{
              		// C's version
              	}
              }
              

              }

              Given this code, instantiate an instance of C and call the I interface version of MethodA without first casting C to an I. AFAIK, it can't be done. So the mechanism we use to differentiate between which version we're calling is a cast to the specific class. Casting allows us to specify whose "vtable" (a table that points to functions) we want to use. Now in the case of a derived class, you can't do something like: DerivedClass d = new DerivedClass(); ((DerivedClass)d).MethodA(); because casting something that's already a given class doesn't really do anything. Casting something to a base class or an interface gives you the "vtable" of that class, which allows us to "specify" a particular method. The problem comes into the fact that the base class's vtable might have a pointer to the derived class in the case the method gets overridden. This is what happens to virtual functions that are redefined with an override. Abstract methods are automatically virtual, they just have no "default" implementation. You are required to supply one in the case of abstract methods. If we can't use casting to "filter" our vtable and specify which method we're calling, we would need some other method of specifying. C++ ends up using the "::" operator to do this. If you want to call a specific parent class version, you just call it with something like: ParentClass::MethodA() It becomes a real mess... :) Does any of this help at all? I'm probably not doing this discussion justice. Have you done any C++ in the past?


              I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monu

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups