Monday, 20 February 2017

inheritence example program in c#

Inheritence example program
/* c# inheretence example program*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication56
{
    using System;

    public class ParentClass
    {
        //class name constructor name should be same(constructor)
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor");
        }
        //method
        public void print()
        {
            Console.WriteLine("I'm a Parent Class");
        }
    }
    //inheretence from parent class
    public class ChildClass : ParentClass
    {
        //class name constructor name should be same
        public ChildClass()
        {
            Console.WriteLine("Child Constructor");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //creating obj for child class
            ChildClass child = new ChildClass();
            //calling parent class method
            child.print();
        }
    }

}


output: screenshot







No comments:

Post a Comment