- 1. What is Inheritance?
- 2. What is the Advantages of Inheritance.
- 3. What are different types of inheritance?
- 4. Is multiple inheritance possible in C#.Why?
- 5. How do you prevent a class from being inherited ?
- 6. What do you mean by sealed keyword ?
- 7. can you mark method as sealed ?
- 8. what do you mean by upcasting and downcasting ?
1.) what is inheritance ?
- It is mechanism of creating new class by already existing class.
- Inheritace is used to establish the relationship between two (or) more classes.
- It is mechanism of obtaining the Variables and Methods from one class to another class.
- The class which is giving Variable & Methods is called as "Base Class (or) Super Class (or) Parent Class".
- The class which is taking variable and methods is called as "Derived Class (or) Sub Class (or) Child Class".
- The Operator : is inheritance operator
- 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"
5.) How do you prevent a class from being inherited ?
Make that class as sealed.
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 .
1 Comments
What is Inheritance?
ReplyDeleteInheritance 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