Tutorials
C Sharp
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;
}
}
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
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
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
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();
}
In this article we discussed Inheritance as implemented in C# and NET We also reviewed the key players involved – Classes, Structs and Interfaces
First Page: Inheritance in C#
| Provided a quick and succinct review of inheritance in c#. |
| Excellent Job by exforsys |
|
Exforsys has done good work for beginners |
| Very good |
| youhave done a good job. it's an faster review of all type of inheritance we can do. really it's an appreciateable job................... GOOD WORK |
| its good |
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
| gud one ......simple and good |
| Article is good and one can easily recals his/her basic concepts of Objected Oriented Programming specific to C#. |
| good one. keep it up |
|
But it creates lot of confidence in me. Thanks keep Writing. |
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
| very good.... keep it up |
|
thanks alot. great tutorial |
|
Very nice and so fast learning! |
| i need it with example.... |
| very good |
| please Give information about every one letest change in C# |
| Gr8 Article You have picked a topic and given an example this is superb |