7/01/2010

C# Single Inheritance



Inheritance:
============
C# supports three types of Inheritance

1) Single inheritance:
----------------------
 A derived class with only one base class.

2) Multilevel inheritance:
--------------------------
 Deriving a new class from an already derived class.

3) Hierarchical inheritabnce:
-----------------------------
 Properties of the base class can be accquired by 2 or more derived classes.


Syntax:
-------   class derived_class_name : visibility_mode base_class_name
   {
  ----------------------
          }
Example:
--------  class beta : alpha {
             -----------
           }  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Single_Inheritance
{
    class alpha
    {
        public int a;
        public void set_a(int a)
        {
            this.a = a;
        }
    }
    class beta : alpha //In c# ':' operator used for inheritance
    {
        public int b;
        public void set_b(int b)
        {
            this.b = b;
        }
        public void print()
        {
            Console.WriteLine("A : " + a + " B : " + b);
            Console.WriteLine("Product : " + (a * b));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            beta b = new beta();
            b.set_a(12);
            b.set_b(3);
            b.print();
        }
    }
}

No comments:

Post a Comment