Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 9 - Inheritance, Abstract Classes and Interfaces


Inheritance

Inheritance is the ability of classes to inherit things like methods and variables from another class. This means that you can create one parent class that has a method in it and all the classes that inherit from the parent class can use that method as if it belongs to them as well.
The best way to explain inheritance is with an example. First we will create a class called ParentClass which has a method in it called SayHello() which prints "Hello" on the screen.
using System;

class ParentClass
{
   public void SayHello()
   {
      Console.WriteLine("Hello");
   }
}


Now we will create another class called ChildClass which will have no methods in it but it will inherit from ParentClass. If you want to inherit from another class you must put a semi-colon after the child class name and then put the name of the parent class to inherit from.
using System;

class ChildClass : ParentClass
{
}

Now we will create a small program that will test ChildClass. You will see that ChildClass has no methods. Through the use of inheritance you can use the SayHello() method that is inherited from ParentClass. Here is the test program:
using System;

class Program
{
   static void Main(string[] args)
   {
      ChildClass cc = new ChildClass();
      cc.SayHello();
      Console.ReadLine();
   }
}

When you run the program you will see that it prints Hello on the screen.

Abstract Classes

An abstract class is just like a normal class except that abstract classes can't be instantiated. You can only inherit from an abstract class.
We will use the ParentClass example from above and change it to an abstract class. The only change you have to make to ParentClass is to put the abstract keyword in front of the class declaration.
using System;

abstract class ParentClass
{
   public void SayHello()
   {
      Console.WriteLine("Hello");
   }
}

Interfaces

An interface is something that is used to make sure that a class implements a certain group of methods. The methods in an interface do not have any code in them and are ended with a semi-colon instead of having curly brackets. You also can't declare variables in an interface.
We will first create an interface called ITest with a method called SaySomething().
using System;

interface ITest
{
   void SaySomething();
}

Next we will create 2 classes called Test1 and Test2. Both of them will implement the ITest interface. You inherit from an interface in the same way as inheriting from a class which is to put it after a semi-colon after the class name. Both classes will have a SaySomething() method but each will print something different on the screen.
using System;

class Test1 : ITest
{
   public void SaySomething()
   {
      Console.WriteLine("Hello");
   }
}

using System;

class Test2 : ITest
{
   public void SaySomething()
   {
      Console.WriteLine("Goodbye");
   }
}

Finally we will need a test program. The test program declares an ITest. A new instance of Test1 is assigned to the ITest and then the SaySomething() method is called on it. A new instance of Test2 is then assigned to the ITest and then the SaySomething() method is called on that one. You will see that even though the same method name is called, it prints something different each time. This is because the interface is using something called polymorphism which allows a method to be called from any class as long as it has the same name. Here is the test program.
using System;

class Program
{
   static void Main(string[] args)
   {
      ITest it;
      it = new Test1();
      it.SaySomething();
      it = new Test2();
      it.SaySomething();
      Console.ReadLine();
   }
}

0 comments:

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India