public class Animal
{
public Animal()
{
}
public string dance(object obj)
{
return obj.ToString()+" Dance on floor.";
}
public string Walk(object obj)
{
return obj.ToString() + " Walk on floor. ";
}
public string sing(object obj)
{
return obj.ToString() + " Sing on floor. ";
}
}
public class Dog:Animal
{
public void dog(){}
public string Run(object obj)
{
return obj.ToString() + "is Runing.";
}
public string Bark(object obj)
{
return obj.ToString() + "is Barking.";
}
}
------ in main method i do the following ------
Animal ani = new Animal();
Dog dogi = new Dog();// i am able to access all the methods in animal and dog
Animal ann = dogi;// here i am able to access only animal methods not dog methods why like "ann.Run();" ?
string abc = ann.dance(ann).ToString();
my question is like this when i create dog object like "Dog dogi=new Dog();", i am able to access all the mothods in Animal and Dog class but when i do it like "Animal ann = dogi;" i am only able to access all methods of Animal but not Dog why like "ann.Run():" ?
Constructors of the base class cannot be inherited by the derived class. Then how come, on executing the statement " Dog d1 = new Dog(); ", the base(ie Animal) class constructor also get called alongwith the derived (ie Dog ) class constructor.
When we are inheriting the Base class means we are able to access all the methods and variables in a base class. That means when we create the instance of the derived class it automatically creates the instance of base class.
New keyword is used to create a new definition of that method, field, or property on a derived class. In Dog class creating new definition of Talk() method. It was in super class(Animal)
Regards,
TweakBird
***** Posted 50000th post in GIT O_O ******
Amit here,I wonder to know about polymorphism without inheritance is it possible?? and if yes can you explain in detail with example...
Thanks, I would appreciate if you can take some time on this....
"When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class."
I believe that if you add extra methods or variables to Dog that the base class Animal does not have... you create a different object that can not be cast as Animal.