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. classes inheritance

classes inheritance

Scheduled Pinned Locked Moved C#
csharpoophelp
5 Posts 3 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
    kabutar
    wrote on last edited by
    #1

    hi, if i have two classes animal(base class) and dog when do we use in the main statement: Animal a1 = new Dog(); because as i undersatnd we create the object of the same class and call methods of different classes after inheritance...ie is Dog a1 = new Dog()..... so how does this statement work:: Animal a1 = new Dog(); can anybody help me thanks

    C#

    N 1 Reply Last reply
    0
    • K kabutar

      hi, if i have two classes animal(base class) and dog when do we use in the main statement: Animal a1 = new Dog(); because as i undersatnd we create the object of the same class and call methods of different classes after inheritance...ie is Dog a1 = new Dog()..... so how does this statement work:: Animal a1 = new Dog(); can anybody help me thanks

      C#

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      kabutar wrote:

      so how does this statement work:: Animal a1 = new Dog();

      Why don't you write a program and experiment this ? well, see the following code

      class Animal
      {
      public virtual void ShowAnimal()
      {
      Console.WriteLine("From animal");
      }
      }

      class Dog : Animal
      {
      public override void ShowAnimal()
      {
      Console.WriteLine("From dog");
      }
      }

      I am initializing the class as you said

      Animal a = new Dog();
      a.ShowAnimal();

      What will be the output ? This will print "From dog". Your object 'a' looks like Animal, but instantiated with Dog. So obviously it will call the overridden method in Dog class. Now remove the overide keyword in Dog class. It will be like

      public void ShowAnimal()
      {
      Console.WriteLine("From dog");
      }

      Now run the application and you will find that it called base class function. Because the object looks like Animal, and instance don't have a method that has relation with Animal. Because child method with the same name without keyword oevrride will be considered as a new method. So it will execute base class method. XMLDocument class having a Save() method which accepts a Stream object. Instead of this, we can pass MemoryStream object which is a child class of stream. So it will call MemoryStream classes methods. Hope things are clear now. Let me know if it helped


      My Website | Ask smart questions

      K 1 Reply Last reply
      0
      • N N a v a n e e t h

        kabutar wrote:

        so how does this statement work:: Animal a1 = new Dog();

        Why don't you write a program and experiment this ? well, see the following code

        class Animal
        {
        public virtual void ShowAnimal()
        {
        Console.WriteLine("From animal");
        }
        }

        class Dog : Animal
        {
        public override void ShowAnimal()
        {
        Console.WriteLine("From dog");
        }
        }

        I am initializing the class as you said

        Animal a = new Dog();
        a.ShowAnimal();

        What will be the output ? This will print "From dog". Your object 'a' looks like Animal, but instantiated with Dog. So obviously it will call the overridden method in Dog class. Now remove the overide keyword in Dog class. It will be like

        public void ShowAnimal()
        {
        Console.WriteLine("From dog");
        }

        Now run the application and you will find that it called base class function. Because the object looks like Animal, and instance don't have a method that has relation with Animal. Because child method with the same name without keyword oevrride will be considered as a new method. So it will execute base class method. XMLDocument class having a Save() method which accepts a Stream object. Instead of this, we can pass MemoryStream object which is a child class of stream. So it will call MemoryStream classes methods. Hope things are clear now. Let me know if it helped


        My Website | Ask smart questions

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

        hi, thanks navneeth ...... letme conclude tis... ie we use this method when we have to override am i right....... hope my conclusion is correct.... thanking you Navneeth

        C#

        N M 2 Replies Last reply
        0
        • K kabutar

          hi, thanks navneeth ...... letme conclude tis... ie we use this method when we have to override am i right....... hope my conclusion is correct.... thanking you Navneeth

          C#

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          If you want to override the default functionality of the base class you can use this. If you are removing override keyword from child class, then use new keyword there to avoid compiler warning.


          My Website | Ask smart questions

          1 Reply Last reply
          0
          • K kabutar

            hi, thanks navneeth ...... letme conclude tis... ie we use this method when we have to override am i right....... hope my conclusion is correct.... thanking you Navneeth

            C#

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #5

            It also lets you store a collection of 'Animals' in a collection without having to have a collecton of dogs, a collection of cats, a collection of llamas, a collection of...I think you know where I am going. You can just have a collection of Animals, and add any class dervied from Animal to it.
            class Animal {}; class Cat : Animal {}; class Dog : Animal {}; List<Animal>_myListOfAnimals = new List<Animal>(); Animal myCat = new Cat(); //assume Cat derived from Animal Animal myDog = new Dog(); //assume Dog derived from Animal _myListOfAnimals.Add( myCat ); _myListOfAnimals.Add( myDog );
            Saying this, if you are using inheritence just to have this ability, then interfaces are the way to go.

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            "This time yesterday, I still had 24 hours to meet the deadline I've just missed today."

            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