Wednesday, 31 May 2017

Inheritance in C#.

One of the most important concepts in object-oriented programming is inheritance.

It also provides an opportunity to reuse the code functionality and speeds up performance time in inheritance, the ability to create classes which inherits certain aspects from parent classes.
When  we create a class, instead of writing fully new data members and member functions, the programmer can assign that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class. 

*** We can only create object of derived class.***

Example:

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

namespace Inheritanse
{
    class Shape  // Shape is  baseclass
    {
       
        public int l = 10;
        public int b = 30;

        class Area : Shape   // Area is derived class. 
        {
            public int h = 50;

            public int getArea()
            {
                return (l * b * h);
            }
        }
        static void Main(string[] args)
        {
            Area Rect = new Area();
           
           Console.WriteLine("Total area: {0}",Rect.getArea());
            Console.Read();
            Console.ReadLine();

        }
    }
}

Share this


3 Comments
avatar

Sangam Packers And Movers :- India No.1 Packing & Moving Services
Url : - sangampackers.com

Reply
This comment has been removed by the author. - Hapus

Advertisement