简体   繁体   中英

does inherited class can access base class

If I have ThumbPhoto entity which inherits Photo entity and Photo entity inherits Entity<int> is it enough to use

public class ThumbnailPhoto : Photo

or should I use

public class ThumbnailPhoto : Photo, Entity<int>

C# doesn't support multiple inheritance, so no.

That aside though, this:

public class ThumbnailPhoto : Photo

Means that you'll have access to the Entity methods/properties because of

public class Photo : Entity<int>

This principle works however deep your inheritance gets.

Multiple inheritance is not allowed in C#. You cannot inherit from more than one class. However hierarchical inheritance is fine. So it is ok to write:

public class ThumbnailPhoto : Photo

and you also derive from Entity<int>

The first is sufficient.

The base classes or interfaces from your base class are automatically inherited.

In fact is Entity isn't an interface, the later isn't even valid, as you can only specify a single base class in C#.

public class ThumbnailPhoto : Photo

Above is an option and sufficient, because nothing would be gained if you could also include Entity<int> assumping Photo is derived from Entity<int>

public class ThumbnailPhoto : Photo, Entity<int>

Above is example of multiple inheritance which is not supported in C#


Do not confuse this with multiple interface implementation, which is supported:

public class ThumbnailPhoto : Photo, IEntity, IAmAnInterface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM