C# INHERITANCE
Inheritance is one of the most important concept about object-oriented programming. Inheritance allows us to define a class in another class, which makes it easier to create and maintain the application. It also provides an opportunity to reuse code functions and speed up implementation time.When creating a class, instead of writing the data members that are truly new and member function, the programmer can define a new class that must be inherited members of a class. The class is called base class and the new class is called as the derived class.
The idea of the inheritance relationship IS implemented-A. For example, mammals are animals, Mammals IS-A dog then the dog IS-A animal, and so on.
Base and Derived Classes
The class can be derived from more than one class or interface, which means that it can inherit the data and functionality from several base class or interface.
The syntax used in C # to create a derived class is as follows:
Consider a base class Shape and its derived class Rectangle:
When the above code is compiled and executed, produces the following results:
Initializing the base class
Derived class inherits the base class member variables and member methods. Therefore the super class object must be created before the subclass is created. Can you give me instructions for initializing the superclass in the member initialization list.
The following program demonstrates this:
When the above code is compiled and executed, produces the following results:
Multiple Inheritance in C#
C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. The following program demonstrates this:
When the above code is compiled and executed, produces the following results:
No comments:
Post a Comment