Consider the Fetus class, which is excreted by two children ( Apple and Banana ), as shown below:
class Fruit { public virtual string GetColor() { return string.Empty; } } class Apple : Fruit { public override string GetColor() { return "Red"; } } class Banana : Fruit { public override string GetColor() { return "Yellow"; } }
We have an existing ICloneable interface in C # . This interface has one method, as shown below, a class that implements this interface ensures that it can be cloned:
public interface ICloneable { object Clone(); }
Now, if I want to make the Apple (not Banana ) class cloned, I can simply implement ICloneable as follows:
class Apple : Fruit , ICloneable { public object Clone() {
Now, considering your argument to a pure abstract class, if C # had a pure abstract class, say Clonable instead of the IClonable interface as follows:
abstract class Clonable { public abstract object Clone(); }
Now can you make your Apple class cloned by inheriting an abstract Clonable instead of IClonable ? eg:
// Error: Class 'Apple' cannot have multiple base classes: 'Fruit' & 'Clonable' class Apple : Fruit, Clonable { public object Clone() { // add your code here } public override string GetColor() { return "Red"; } }
No, you cannot, because a class cannot be obtained from several classes.
Javid Ahmad Nov 12 '17 at 10:23 2017-11-12 10:23
source share