In an interview it was asked why do we need to override method of base class. I tried to answer like when we want to have different implementation in derived class. But then he said, "Why don't we just create a new method with different name and different implementation instead of overriding base class method?", anyway we are not reusing base class method as implementation will be different then just create a new method instead of overriding.
I got confused what to answer. Could somebody explain.
public class BaseClass { virtual void Foo(){} } public class DerivedClass: BaseClass { override void Foo(){} } Generally we implement overriding like above. What he said is like why do we need concept of overriding we can do like below
public class BaseClass { void Foo(){} } public class DerivedClass: BaseClass { void Foo1(){} } His question was looking weird I tried to explain but like its a method of base class we are just redefining it in derived class. In this way our code will be clean as well. But looks like he was not satisfied.
Animal a = new Dog(); Animal b = new Cat(); a.Speak(); b.Speak();- how is it different toDog a = new Dog(); Cat a = new Cat(); a.Speak(); b.Speak();?Foo1should beFooto show shadowing vs. virtual methods... So far the post is way too broad - hopefully it somewhat covered by 3 duplicates I selected.