Showing posts with label example of c# inheritance. Show all posts
Showing posts with label example of c# inheritance. Show all posts

Friday, February 26, 2016

What is inheritance in C#?



Inheritance is one of the fundamental object oriented programming language concept that allow the creation of hierarchical classifications.in C# programming the, a class that is inherited is called base class. In simple words the child class inherit variables, methods, properties and indexers of the parent class by using the “:” character. The main advantage inheritance is to re-usability of code.

The example of the inheritance is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inheritance
{
    public class Class1
    {
        public void xyz()
        {
            MessageBox.Show("Hello");
        }
    }

    public class pqr : Class1
    {
        public void xyz1()
        {
            MessageBox.Show("Hi");
        }
    }
}

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inheritance
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pqr obj = new pqr();
            obj.xyz();
            obj.xyz1();
        }
    }
}
 

output: Hello
Hi