简体   繁体   中英

Is it possible in C# to inherit a subclass from a subclass?

I'm studying OOP at the moment, and I've got this thesis. Because I know that inheriting a subclass from a superclass be like: class superClass : subClass{ } but I'm curious if this is an acceptable form: class superClass : subClass1 : subclass2 .

In a nutshell, I want to inherit a subclass from another one. Because what if I get into a problem like there is a subclass and it is inherited from a superclass because they have common data. Thats fine right?

But here is the catch: What if I need another subclass, which has common data with both of the first subclass and the super or main class?

You've got it backward. Subclasses derive from superclasses, by definition.

class SubClass : Superclass
{
}

Or

class Dog: Animal
{
}

If you want to derive from a subclass that is derived from a superclass, you just need to derive from the subclass:

class SubClass : Superclass
{
}

class SubClass2 : Subclass
{
}

Or

class Dog: Animal
{
}

class Terrier: Dog
{
}

By declaring that a Terrier is a type of Dog, you are also declaring it is a type of Animal, and will have access to all of Animal's methods and properties.

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