October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Structures, Enumerations, and Inheritance in C#

  • October 10, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Inheritance and Abstract Classes

The relationship between two or more classes is termed as Inheritance in an Object-Oriented programming language. Normally, there will be one class, from which the other classes may derive. The former class is called as Base class or super class and the latter class is called the derived class. All variables and methods in the base class can be called in the derived classes, provided they are declared public or protected. In C#, classes are extended by means of the : operator. Consider the figure given below:



Click here for a larger image.

The relationship in the above figure can be expressed in C# as shown in Listing 6:

Listing 6

public class COMPUTER
{
  //code goes here
}

class COMPAQ:COMPUTER
{
  //code goes here
}

class DELL: COMPUTER
{
  //code goes here
}

C# doesn't support multiple inheritance. Hence, the piece of code shown in Listing 7 is illegal in C#:

Listing 7

Public class COMPUTER: COMPAQ, DELL
{
  //code goes here
}

C# introduces a new concept called Interfaces (to be discussed later), which is regarded as an alternative to Multiple Inheritance.

Sealed Classes

When a base class is declared with the sealed keyword (see Listing 8), that class cannot be extended. This is same as the final keyword in Java.

Listing 8

public sealed class Computer
{
  //code goes here
}

class COMPAQ:COMPUTER
{
  //Not allowed as base class is sealed
}

Abstract Classes

The Abstract class is a special type of class that should be declared with the abstract keyword. Moreover, it should contain one or more abstract methods, which should contain only method definitions. It won't have any method body (in the form of curly braces) as do the Instance and Static methods.

Normally, a base class is declared with the abstract keyword and the derived classes should extend the abstract class and implement relevant methods. Keep in mind that only one abstract class can be extended at a time because C# won't support multiple inheritance. Listing 9 illustrates this concept clearly:

Listing 9

using System;

  abstract public class Absdemo
  {
    public abstract void Show();
  }

class Absimp:Absdemo
{
   public override void Show()
   {
     Console.WriteLine("Abstract Method Implemented");
   }

   public static void Main(string[] args)
   {
     Absimp ai = new Absimp();
     ai.Show();
   }
}

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.

# # #





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel