Scenario-
Abstract class without abstract members.
Function present in both base class and child class. This concept is called Method hiding.
public class MyClass
{
public void foo()
{
Console.WriteLine("Base version of foo");
}
}
public class MyChild : MyClass
{
public new void foo()
{
Console.WriteLine("Child version of foo");
}
}
Without new keyword compiler will give a warning that you are hiding the base class implementation.
We will create objects of base and child normally and through casting in all following code samples:-
MyClass obj = new MyClass(); //Not possible
MyChild obj2 = new MyChild();
MyClass objCastedFromChild = (MyClass)obj2;
obj2.foo();
objCastedFromChild.foo();
Output:-
Child version of foo
Base version of foo
so in normal scenario’s follow the simple rule. Thy object thy function.
So now you must have a good idea about what an abstract class is.
Interview definition:-
Abstract class is a class which can’t be instantiated. By instantiation we mean that we can’t create objects through constructor calling on base but we can create objects through casting.
Usage:-
Handy for building big architectures where all functionality is not known in initial stages so architect compiles whatever his knowledge is about the subject into class definition and declares what he thinks should be there but for which he has no concrete ideas as abstract.
.Net doesn’t support multiple inheritances. This is because the gains that were there for multiple class inheritance were very less compare to the cost of resolving ambiguity or name collisions in cases where two base classes have data or functions with same name so .Net removed multiple inheritances through classes. Now you can have data from just one base class but you can implement multiple interfaces.