0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Are you studying "OOP" or "OOP in C#"? There are differences. – Enigmativity Feb 16 '19 at 03:52
  • 1
    Not sure who picked that question as duplicate, but it's a different case. This question is about straightforward sub-subclassing. The question linked as duplicate deals with the complex mess of trying to inherit from multiple classes. – Nyerguds Feb 16 '19 at 13:45
  • The question you are asking deals with the concept of multiple inheritance.Basically you are asking is that class A inherits from Class B.So can class C inherit from class B.Yes Class C can inherit from class B.This is multiple inheritance.I think you got confused with "Multiple Inheritance" and "MultiLevel Inheritance". – CodeOfLife Feb 16 '19 at 14:55

1 Answers1

4

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.

John Wu
  • 50,556
  • 8
  • 44
  • 80