C#: INHERITACE Interview Question and Answer


1.) what is inheritance ?

  1. It is mechanism of creating new class by already existing class.
  2. Inheritace is used to establish the relationship between two (or) more classes.
  3. It is mechanism of obtaining the Variables and Methods from one class to another class.
  4. The class which is giving Variable & Methods is called as "Base Class (or) Super Class (or) Parent Class".
  5. The class which is taking variable and methods is called as "Derived Class (or) Sub Class (or) Child Class".
  6. The Operator : is inheritance operator
  7. Inheritance is always is possible between "Base Class" to "Derived Class"

2.) What is the Advantages of Inheritance.

The Advantages of Inheritance is :

  • Reusability
  • Re-Implementaion
  • Extensability

3.) What are different types of inheritance?

  • Single Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
  • Multiple Inheritance

4.) Is multiple inheritance possible in C#.Why

No. because

  • Its not supported by CLR since its support many diff language and not all languages can have multiple inheritance concept.
  • Because of the complexities involved where method name can clash when two diff classes have same method name.This is resolved by pointers in C++ but its not possible in c#.
  • In c#.net we can achieve multiple inheritance by using "Interfaces"


6.) What do you mean by sealed keyword ?

If you mark a class as sealed it means that you cannot inherit from it but you can create objects of that class.


7.) can you mark method as sealed ?

Yes.But for a method to be marked as sealed you need to have override keyword also.


8. what do you mean by upcasting and downcasting ?

class DerivedClass :BaseClass

Upcasting--assigning a derived class object to a base class.This is implicit.
BaseClass b= new DerivedClass.


Downcasting--assigning baseclass object to derived class. This is explicit and it throws run time error .
a)BaseClass b = new BaseClass()
   DerivedClass d= b  //will give compile time error.
  
b) d=(DerivedClass)b  //will give runtime error.


c)b=d;
   d=(DerivedClass)b;
   d.method()             //will always call derivedclass method

   //there is no point in taking so much of pain and using this kind of code !!!

Upcasting -- assigning a derived class object to a base class.This is implicit. Downcasting -- assigning baseclass object to derived class. This is explicit and it throws run time error .

Please Share Your Interview Question & Answer related to Inheritance Topic Only

Post a Comment

1 Comments

  1. What is Inheritance?
    Inheritance with example's and Types of Inheritance
    1.) Single Inheritance
    2.) Multi-Level Inheritance
    3.) Hierarchical Inheritance
    4.) Hybrid Inheritance
    5.) Multiple Inheritance

    http://allittechnologies.blogspot.com/2016/01/inheritance-with-example-in-dotnet-programmer.html

    ReplyDelete