Microsoft Interview Question
Software Engineer in TestsTeam: IE
Country: United States
I think it could be thinked in two aspects.
First, from the perspective of philosophy, abstract class could not represent a real object. Say if someone asks you to buy a fruit, but don't tell you which kind of fruit, nothing could be bought.
Second, supposing an abstract class can be instanced, then the compiler needs to know how to handle a symbol like virtual void foo()=0; Here the symbole foo doesn't have a body, so compiler cannot put it in .text section, therefore it doesn't have address, just a null symbol. Further more, compiler also needs to ensure that no function calls is made to foo(). So a class has an instance but some of its methods cannot be accessed (we cannot just force all pure virtual functions must be declared as private), what it looks like? A black hole in language.
Lets say,we have an abstract class named Vehicle.We may use the fields,properties and member functions defined within this abtract class to create child classes like Car,Truck, Bike etc. that inherit the features defined within abstract class.To prevent directly creating an instance of the class Vehicle,we make use of the abstract keyword.To use the definitions defined in the abstract class, the child class inherits from the abstract class, and then the instances of the child class may be easily created.
Abstract class cannot instantiate means it cannot create a object!
- ravi March 02, 2012Lets say a Shape abstract class, has triangle and circle derived classes.
you cannot have a object which is just a shape, it MUST BE either a triangle or a circle..
the reason for abstract class is to put this MUST BE condition!