HomeC# C# Simplified, part 4: Structures, Inherit...
C# Simplified, part 4: Structures, Inheritance and Interfaces
In this article, the fourth in a series covering C#, you will learn about structures, inheritance, and interfaces, with the help of illustrative source code. If you ever wondered what C# uses instead of multiple inheritance, keep reading.
C# Simplified covers each and every concept of the C# programming language in a concise manner. The articles in this series have been divided into several parts and will provide detailed explanations along with source codes and screenshots. This series has been specifically written for beginners and students with an aim to teach C# in a quickest possible time. Please send your comments to csharpsimplified@gmail.com
If you are familiar with C++, you should be familiar with structures. Structures are value types, and are defined by using the struct keyword. You should create an object of the structure in order to access the variables inside them. In listing 4.1, a structure named EmpStruct is created with two variables. Inside the Main() method, an object of the structure is created, and the appropriate variables are called and printed.
Properties are new to C#. They provide added functionality to the .NET Framework itself. If you had worked with Visual Basic 6.0, you might have used methods like getXXX() and setXXX() extensively for creating user defined properties while working with ActiveX.
A simple C# property consists of a field declaration and accessor methods like get and set. The statements inside the get block are used to retrieve the field's value, and those inside the set block are used to modify the field's value. C# uses a special keyword named value to modify data.
In listing 4.2, a field named MyValue is declared. It also declares a property named PropValue and assigns the field variable appropriately by using the get and set blocks. Inside the Main() method, an instance of the class is created and the property is accessed and printed.
Listing 4.2
using System;
class PropertiesDemo
{
// Field "MyValue" declared
public string MyValue;
// Property declared
public string PropValue
{
get
{
return MyValue;
}
set
{
MyValue = value;
}
}
public static void Main()
{
// Instance of the class created
PropertiesDemo pd = new PropertiesDemo();
// Property accessed
pd.PropValue = "101";
string p = pd.PropValue;
Console.WriteLine("The Value is {0}",p);
}
}
In the above code, the PropValue property is considered to be a read and write property because both getter and setter blocks are defined. If you want to create a read only property you should omit the set block; to create a write only property, you should omit the get block.
Inheritance is the relationship between two or more classes, and is a fundamental concept in every Object Oriented Programming (OOP) language. There will be one class which will act as a base class. The other classes will derive from the base class. This class is termed a derived class. You can call all the variables and methods defined in the base class, in the derived classes. The only condition is that you should declare them as either public or protected. In C#, classes are inherited with the help of the ":" operator.
The following code snippets will help you to understand the concept in more detail.
public class Computer
{
//code goes here
}
class HardDrive:Computer
{
//code goes here
}
class MotherBoard:Computer
{
//code goes here
}
Listing 4.3 defines a class named Computer and a protected method named Calls. The method returns the sum of two numbers. The class HardDrive inherits the base class Computer and calls the method Calls() by creating an object of the derived class.
Listing 4.3
// HardDrive.cs
using System;
class Computer
{
// Erase the Protected modifier and observe the result.
protected void Calls()
{
int num1 = 10;
int num2 = 20;
int num3 = num1+num2;
Console.WriteLine(num3);
}
}
class HardDrive: Computer
{
public static void Main()
{
HardDrive hd = new HardDrive();
hd.Calls();
}
}
C# does not support multiple inheritance. Hence, the following code snippet is not allowed in C#.
Public class Computer: HardDrive, MotherBoard
{
//code goes here
}
In order to rectify the problem caused by multiple inheritance, C# introduces a new concept called interfaces. Interfaces in C# are similar to Java interfaces. You will learn more about interfaces in the final part of this article.
Classes declared with the sealed keyword cannot be extended. That means, when you declare a base class with this keyword, you cannot use it in a derived class. In other words the class cannot be extended. This keyword is similar to that of final keyword in Java.
Listing 4.4 is a modified version of listing 4.3. It declares the base class Computer as sealed.
Listing 4.4
// HardDriveSealed.cs
using System;
// error location
sealed class Computer
{
// Erase the Protected modifier and observe the result.
public void Calls()
{
int num1 = 10;
int num2 = 20;
int num3 = num1+num2;
Console.WriteLine(num3);
}
}
class HardDriveSealed: Computer
{
public static void Main()
{
HardDrive hd = new HardDrive();
hd.Calls();
}
}
When you execute the above code, you will get a compilation error indicating that the derived class cannot inherit from the sealed base class Computer.
The abstract class is a special type of class which contains one or more abstract methods. It should be declared with the keyword abstract. Every abstract class consists of one or more abstract methods. These methods will only have the method definitions. The abstract class will not have any method body, such as the instance method or the class method.
Basically, a base class is declared with the abstract keyword, and the derived classes should inherit the abstract class and provide implementation for the relevant methods. In listing 4.5, an abstract method named Display() is declared inside the AbstractDemo class. The derived class then inherits the abstract class and provides implementation to the abstract method. Inside the Main() method, an instance of the class is created and the method is called.
As explained previously, C# does not support multiple inheritance. In order to rectify this drawback, Microsoft introduced interfaces into C#. They are regarded as an alternative to multiple inheritance. If you have experience with Java, you should be right at home. All interfaces should be declared with the keyword Interface. You can implement any number of interfaces in a single derived class. A method declared inside the interface doesn't contain any method definition. Instead, the implementing class should provide functionality for the corresponding method.
In listing 4.6, an interface named InterfaceDemo is declared with two method definitions. The derived class, InterfaceImp, implements these two methods. Inside the Main() method, an instance of the class is created and the methods are called appropriately.
The C# programming language introduces a unique feature with which you can combine interfaces. That means two or more interfaces can be combined into a single interface. After that you only need to call the combined interface while inheriting them. In listing 4.7, two interfaces named InterfaceDemo and InterfaceDemo1 are combined into a single interface called CombinedInter. The class MultipleInter extends this interface and provides the implementation of the interface methods. Inside the Main() method, the methods are called by creating an instance of the class.