An abstract base class (ABC) can have data to support the classes that inherit form it. However, given that its not possible to instantiate an object of 开发者_StackOverflowan ABC how does the compiler handle this data for cases where we have a number of derived class objects that inherit the ABC. Does the data become associated with the derived class object?
If you are talking about static data, then that data will remain assocciated with the base class. There will still only be one instance of that data in memory no matter how many different classes derive from it.
Non-static data will be associated with each instance of that class. If you create 5 instances of that class, there will be 5 instances of that data in memory, each only accessible through its associated instance.
Yes.
A compiler can only accept an inherited type that has been instantiated with derived, substantial class. This is because
public void RunInstantiate()
{
IAbstract abc;
abc = new Implement();
}
abc
will always point to a real object (of the type Implement
).
精彩评论