Technical Training
C Sharp TutorialsInheritance in C# Page - 2
Inheritance in C#
Ads
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 Classes and Functions example:
abstract class Car
{
public int headlights = 2;
public abstract price();
}
Sealed Classes:
If a class is declared as Sealed you cannot inherit from that class
Declaring a method as sealed prevents it from being overridden
sealed class LastManStanding
{
}
The compiler gives an error if any other class tries to derive from the above class
class bankManager
{
public sealed override bool authorize()
{
}
}
The compiler gives an error if you override the above method
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 modifer If a member or type is prefixed with public it is visible to all the code
protected: It can be used for any member or a nested type This causes the member/nested type to be visible to any derived type
private: This can be used for any type or member and the member/type will be visible only inside the type where it was defined
internal: This causes the member/nested type to be visible within the assembly where it is defined
protected 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
Defining an Interface
We can define an interface as follows:
public interface IbankManager
{
bool authorize();
}
Implementing an Interface
The above interface can be implemented as follows:
public class newManager:IbankManager
{
public bool authorize()
{
//process
return true;
}
}
Deriving an Interface
Interfaces can be derived from other interfaces
Example:
public interface IbranchManager:IbankManager
{
public string sendReports();
}
Summary:
In this article we discussed Inheritance as implemented in C# and NET We also reviewed the key players involved – Classes, Structs and Interfaces
C Sharp Tutorials
- Structural and Behavioral Design Patterns
- Creational Design Patterns
- Software Architecture & Design Patterns
- C# Language Basics
- . NET Type Safety
- Regular Expressions in C# - Quantifiers and Delegates
- Regular Expressions and C#, .NET
- .NET Remoting
- Inheritance in C#
- Delegates in C#
- Building Web Based N-Tier Applications using C#







