Menu Close

INHERITANCE IN C# WITH SAMPLE EXAMPLE

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

namespace ConsoleApplication4
{
    /// <summary>
    /// Base class Employee( Inheritance example)
    /// </summary>
    public class Employee
    {
        public string FirstName;
        public string LastName;
        public string Email;
   
        public void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName);
        }
    }
    /// <summary>
    /// Derived class inheriting Employee
    /// </summary>
    public class FullTimeEmployee : Employee
    {

      public  float YearlySalary;
    }
    /// <summary>
    /// Derived class inherting Employee
    /// </summary>

    public class PartTimeEmployee :  Employee
    {
     public  float HourlyRate;

    }
    class Program
    {
        static void Main(string[] args)
        {
            FullTimeEmployee FTE = new FullTimeEmployee();
            FTE.FirstName = “Mitali”;
            FTE.LastName = “Gurram”;
            FTE.Email = “malla.gurram@gmail.com”;
            FTE.YearlySalary = 100000;
            FTE.PrintFullName();
            Console.ReadLine();

            PartTimeEmployee PTE = new PartTimeEmployee();
            PTE.FirstName = “GMR”;
            PTE.LastName = “IT SOLUTIONS”;
            PTE.HourlyRate = 90;
            PTE.PrintFullName();
            Console.ReadLine();
        }

    }
}

=============
DERIVED CLASS  CAN CONTROL BASE CLASS CONSTRUCTORS

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

namespace ConsoleApplication4
{
    public class ParentClass
    {     //parent constructor
        public ParentClass()
        {
            Console.WriteLine(” parentclass constructor called”);
            Console.ReadLine();
        }
        public ParentClass(string Message)
        {
            Console.WriteLine(Message);
            Console.ReadLine();
        }
    }

    public class ChildClass : ParentClass
    {    //childclass constructor
        public ChildClass() :  base(“Derived class controlling parent class”)
        {
            Console.WriteLine(“childclass constructor called”);
             Console.ReadLine();
        }
    }

   
    class Program
    {
        static void Main(string[] args)
        {
           ChildClass CC = new ChildClass();
           
        }

    }
}

Share this:
Posted in Blog

Related Posts