Inheritance

Inheritance

What exactly is Inheritance? MSDN explanation says : Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

So what can we inherit? For example we have Car and Motorcycle classes, and as we know cars and motorcycles have a few things in common such as engine and color. We could create a base class named Vehicle that has those properties and make Car and Motorcycle classes which will inherit Vehicle class.

 

Can i inherit from multiple classes? No, but you can Inherit multiple Interfaces that inherit one class.

 

I don’t wish for my class or method to be directly instantiated. What can I do about that problem? You can make those classes or methods abstract. Beware, when you make your class abstract one, none of your class members have to be marked as abstract, but if at least one of your members is marked as abstract, entire class must be marked as abstract.

 

Can derived class access private fields of base class? No, it can not. You can only access internal, public, protected, and protected internal members of a base class.

 

Is it possible to prevent our class from becoming other classes base class? Yes, mark that class as sealed and other classes cannot inherit from this class.

2 Comments

  1. This site is really cool. I have bookmarked it.

    Do you allow guest posting on your site ?

    I can provide hi quality posts for you. Let me know.

Leave a Reply

Your email address will not be published.

*

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑