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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Inheritage with multiple abstract classes

Inheritage with multiple abstract classes

Scheduled Pinned Locked Moved C#
question
3 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.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    I´ve got a question regarding inheritage with multible abstract classes. Say I´ve got these classes:

    public abstract class Animal
    {
    public abstract void MyMethod;
    }

    public abstract class Mammal : Animal
    {
    public abstract void MyMethod;
    }

    public class Dog : Mammal
    {
    public override void MyMethod()
    {...}
    }

    Animal and Mammal are both abstract. Now I dont want the method MyMethod() implemented in Mammal, but I want it implemented in Dog. How can I do? I know the code above doesnt compile.

    T B 2 Replies Last reply
    0
    • T TMattC

      I´ve got a question regarding inheritage with multible abstract classes. Say I´ve got these classes:

      public abstract class Animal
      {
      public abstract void MyMethod;
      }

      public abstract class Mammal : Animal
      {
      public abstract void MyMethod;
      }

      public class Dog : Mammal
      {
      public override void MyMethod()
      {...}
      }

      Animal and Mammal are both abstract. Now I dont want the method MyMethod() implemented in Mammal, but I want it implemented in Dog. How can I do? I know the code above doesnt compile.

      T Offline
      T Offline
      TMattC
      wrote on last edited by
      #2

      Ahem, just found the answer by myself. I leave the method out altogether in the Mammal class.

      1 Reply Last reply
      0
      • T TMattC

        I´ve got a question regarding inheritage with multible abstract classes. Say I´ve got these classes:

        public abstract class Animal
        {
        public abstract void MyMethod;
        }

        public abstract class Mammal : Animal
        {
        public abstract void MyMethod;
        }

        public class Dog : Mammal
        {
        public override void MyMethod()
        {...}
        }

        Animal and Mammal are both abstract. Now I dont want the method MyMethod() implemented in Mammal, but I want it implemented in Dog. How can I do? I know the code above doesnt compile.

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        Try this:

        namespace YourNameSpace
        {
        public abstract class Animal
        {
        public string Name;

            public abstract string MyAbstractMethod();
        
            public virtual string MyVirtualMethod()
            {
                return string.Format("Animal: {0}", Name);
            }
        }
        
        public class Mammal : Animal
        {
            public override string MyAbstractMethod()
            {
                return string.Format("Mammal: {0}", Name);
            }
        
            public new string MyVirtualMethod()
            {
                return string.Format("Mammal: {0}", Name);
            }
        }
        
        public class Dog : Mammal
        {
            public new string MyAbstractMethod()
            {
                return string.Format("Dog: {0}", Name);
            }
        
            public new string MyVirtualMethod()
            {
                return string.Format("Dog: {0}", Name);
            }
        }
        

        }

        Test it like this:

        namespace YourNameSpace
        {
        private void TestAnimal()
        {
        Dog myDog = new Dog();
        myDog.Name = "Spot";

            string dname = myDog.MyAbstractMethod();
        
            string mname = (myDog as Mammal).MyAbstractMethod();
        
            string aname = (myDog as Animal).MyAbstractMethod();
        
            string dname2 = myDog.MyVirtualMethod();
        
            string mname2 = (myDog as Mammal).MyVirtualMethod();
        
            string aname2 = (myDog as Animal).MyVirtualMethod();
            **// set a break-point here and observe variable values:
            // repeat until 'understanding' happens :)**
        }
        

        }

        «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

        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