Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

Inheritance in C#

By Exforsys | on November 11, 2005 |
C Sharp

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

Classes 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 parameters

Interfaces:

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 interface

Inheritance:

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 Inheritance

What is Interface Inheritance?

– When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance

In 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

private struct childStruct:InterfaceX, InterfaceY
{
}

Virtual Methods:
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;
    }
}

This is useful because the compiler verifies that the ‘override’ function has the same signature as the virtual function

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 someBaseClass
{
 
}
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

The C# compiler will generate a warning in this case The new keyword should be used when we intend to hide a method

Example:

class grandchildClass: 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 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

« « Building Web Based N-Tier Applications using C#
Test Cases, Test Scenarios Interview Questions » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Ads

Free Training

RSSSubscribe 430 Followers
Ads
  • Popular
  • Recent
  • Regular Expressions in C# – Quantifiers and Delegates

    November 14, 2005 - 0 Comment
  • . NET Type Safety

    November 15, 2005 - 0 Comment
  • C# Language Basics

    November 23, 2005 - 0 Comment
  • Software Architecture & Design Patterns

    November 26, 2005 - 0 Comment
  • Creational Design Patterns

    November 26, 2005 - 0 Comment
  • Structural and Behavioral Design Patterns

    November 26, 2005 - 0 Comment
  • Delegates in C#

    November 8, 2005 - 0 Comment
  • Building Web Based N-Tier Applications using C#

    November 8, 2005 - 0 Comment
  • Regular Expressions and C#, .NET

    November 14, 2005 - 0 Comment
  • .NET Remoting

    November 15, 2005 - 0 Comment
  • Structural and Behavioral Design Patterns

    November 26, 2005 - 0 Comment
  • Creational Design Patterns

    November 26, 2005 - 0 Comment
  • Software Architecture & Design Patterns

    November 26, 2005 - 0 Comment
  • C# Language Basics

    November 23, 2005 - 0 Comment
  • . NET Type Safety

    November 15, 2005 - 0 Comment
  • .NET Remoting

    November 15, 2005 - 0 Comment
  • Regular Expressions in C# – Quantifiers and Delegates

    November 14, 2005 - 0 Comment
  • Regular Expressions and C#, .NET

    November 14, 2005 - 0 Comment
  • Building Web Based N-Tier Applications using C#

    November 8, 2005 - 0 Comment
  • Delegates in C#

    November 8, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Structural and Behavioral Design Patterns
  • Creational Design Patterns
  • Software Architecture & Design Patterns
  • C# Language Basics
  • . NET Type Safety

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2019. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy

Necessary Always Enabled

Non-necessary