classes inheritance
-
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#
-
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#
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 withDog
. So obviously it will call the overridden method inDog
class. Now remove the overide keyword inDog
class. It will be likepublic 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 withAnimal
. Because child method with the same name without keywordoevrride
will be considered as a new method. So it will execute base class method.XMLDocument
class having aSave()
method which accepts a Stream object. Instead of this, we can passMemoryStream
object which is a child class of stream. So it will callMemoryStream
classes methods. Hope things are clear now. Let me know if it helped
-
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 withDog
. So obviously it will call the overridden method inDog
class. Now remove the overide keyword inDog
class. It will be likepublic 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 withAnimal
. Because child method with the same name without keywordoevrride
will be considered as a new method. So it will execute base class method.XMLDocument
class having aSave()
method which accepts a Stream object. Instead of this, we can passMemoryStream
object which is a child class of stream. So it will callMemoryStream
classes methods. Hope things are clear now. Let me know if it helped
-
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#
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 usenew
keyword there to avoid compiler warning.
-
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#
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."