Exforsys.com
 

Sponsored Links

 

C Sharp Tutorials

 
Home Tutorials C Sharp
 

Inheritance in C#

 

Inheritance in C# - Page 2

Page 2 of 2

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




First Page: Inheritance in C#


Read Next: Regular Expressions and C#, .NET



 

 

Comments


Stein said:

  Provided a quick and succinct review of inheritance in c#.
February 10, 2007, 10:41 pm

Subramaniyan T said:

  Excellent Job by exforsys
March 8, 2007, 2:59 am

Shailesh Wath said:

  Exforsys has done good work for beginners
May 5, 2007, 5:58 am

Priyap said:

  Very good
October 9, 2007, 3:47 am

Amandeep Singh Maan said:

  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
October 10, 2007, 7:55 am

Md Salman Arafath said:

  its good
November 8, 2007, 4:35 am

Ali Mardan said:

  i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts.
December 17, 2007, 11:59 pm

Manobala said:

  gud one ......simple and good
December 21, 2007, 12:27 am

Muhammad Aqeel Akram said:

  Article is good and one can easily recals his/her basic concepts of Objected Oriented Programming specific to C#.
March 10, 2008, 2:42 am

Khushi Desai said:

  good one. keep it up
April 30, 2008, 2:02 am

Puneet Goyal said:

  But it creates lot of confidence in me.
Thanks
keep Writing.
May 5, 2008, 1:21 am

Varsha Yadav said:

  i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts.
May 19, 2008, 7:05 am

Hitesh Desai said:

  very good.... keep it up
June 3, 2008, 4:51 am

khaled mohamed said:

  thanks alot.
great tutorial
June 12, 2008, 6:58 am

Nick said:

  Very nice and so fast learning!
August 11, 2008, 11:42 am

karthikeyan.L said:

  i need it with example....
November 23, 2008, 11:27 pm

Changalrao said:

  very good
December 12, 2008, 10:48 pm

1 said:

  please Give information about every one letest change in C#
December 31, 2008, 12:20 am

Ashish Sehra said:

  Gr8 Article You have picked a topic and given an example this is superb
January 2, 2009, 12:44 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape