Inheritance in C#
This article discusses Inheritance concepts in the context of C# Before we understand Inheritance in C# it is important to understand the key players involved, viz Objects, Classes and Structs lasses and Structs are ‘blue-prints’ or templates from which we instantiate (create) objects Example a car may be created based on its blue print Car is the object and blue print is the class (or template)What are types?
An object can be of the following types – Class or Struct There are many differences between the two ‘types’ The main difference between the two is the way in which they are stored in memory and the way they are accessed Classes are also called reference types Structs are known as value types Classes are stored in a memory space called ‘heap’ and Structs are stored in a memory space known as ‘stack’Constructors:
In C#, (like other Objected Oriented languages) constructor is a method having the same name as the class The constructor is called when the object is being created It can have one or more parametersInterfaces:
In the context of C#, an interface provides a contract A class that is derived from this interface will implement the functions specified by the interfaceInheritance:
C# supports two types of Inheritance mechanisms 1) Implementation Inheritance
2) Interface Inheritance
What is Implementation Inheritance?
- When a class (type) is derived from another class (type) such that it inherits all the members of the base type it is Implementation InheritanceWhat is Interface Inheritance?
- When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface InheritanceIn general Classes can be derived from another class, hence support Implementation inheritance At the same time Classes can also be derived from one or more interfaces Hence they support Interface inheritance Structs can derive from one more interface, hence support Interface Inheritance Structs cannot be derived from another class they are always derived from SystemValueType
Multiple Inheritance:
C# does not support multiple implementation inheritance A class cannot be derived from more than one class However, a class can be derived from multiple interfaces Inheritance Usage Example:
Here is a syntax example for using Implementation Inheritance
Class derivedClass:baseClass
{
}
derivedClass is derived from baseClass
Interface Inheritance example:
private Class derivedClass:baseClass , InterfaceX , InterfaceY
{
}
derivedClass is now derived from interfaces – InterfaceX, InterfaceY. Similarly a struct can be derived from any number of interfaces.
{
}
If a function or a property in the base class is declared as virtual it can be overridden in any derived classes
Usage Example:
class baseClass
{
public virtual int fnCount()
{
return 10;
}
}
class derivedClass :baseClass
{
public override int fnCount()
{
return 100;
}
}
Hiding Methods:
Similar to the above scenario if the methods are declared in a child and base class with the same signature but without the key words virtual and override, the child class function is said to hide the base class function
{
}
class abcClass:someBaseClass
{
public int fnAge()
{
return 99;
}
}
class grandchildClass: abcClass
{
public int fnAge()
{
return 10;
}
}
In the example above the function fnAge in grandChildClass hides the function fnAge in its parent class ie abcClass
{
public new int fnAge()
{
return 10;
}
}
Calling Functions from the Base Class:
To call a function from the base class simply use the key word basefunctionname()class basicMember
{
public virtual float membershipFee()
{
return 100;
}
}
class vipMember:basicMember
{
public override float membershipFee()
{
return 200;
}
public float promoMembershipFee()
{
return basemembershipFee() + 60;
}
}
What are Abstract Classes?
1) An abstract class cannot be instantiated 2) An abstract class can have one or more abstract functions
3) Abstract functions are virtual
4) They do not have any implementation
5) They need to be overridden and implemented in a derived non-abstract class
abstract class Car
{
public int headlights = 2;
public abstract price();
}
If a class is declared as Sealed you cannot inherit from that class
Declaring a method as sealed prevents it from being overridden
{
}
{
public sealed override bool authorize()
{
}
}
Constructors and Inheritance:
C# allocates a default zero parameter constructor to every class that does not have any explicit constructors defined If you instantiate a child class, all the constructors in the hierarchy are called The base call constructor is called first and then the next child class constructor This sequence continues until all the constructors are called If an explicit constructor is defined for a class anywhere in the hierarchy there is a possibility that the above chain is broken If this is the case the compiler raises an Error and the code will not compile For instance, if you declare an explicit constructor with one or more parameters the above described sequence of calls to constructors in the class hierarchy which was being handled automatically is now broken This is because when you supply a constructor C# does not provide a default constructor In this case, we have to explicitly maintain the ‘chain’ Another scenario for this error is when you define an explicit constructor with zero parameters and mark it as private The compiler will raise an error in this case
Visibility Modifiers in C#:
public: Any types or members can be prefixed with this modifier If a member or type is prefixed with public it is visible to all the codeprotected or internal: This causes the member/nested type to be visible within the assembly where it is defined and any derived type
Interfaces:
When a class derives from an Interface it implements the functions specified by the interface{
bool authorize();
}
{
public bool authorize()
{
//process
return true;
}
}
Deriving an Interface
{
public string sendReports();
}
Summary:
Sources: Exforsys ,
No comments:
Post a Comment