Portfolio

Tuesday, October 20, 2015

Inheritance C#



What is Inheritance?

Inheritance allows you to create new classes to reuse or extend it at a later time when you know it will derive from the base class.  It also allows you to modify the behavior in the other classes.  The class that is inherited from its members is called the base class.  Moreover, the class that is inherited from the base is called the derived class.

I know all of this sounds a bit confusing at first, but trust me when you start writing it over and over you get used to the style.
 
In this example, I have created an Employee class for full time employee and part time employee, which is related to the base class Employee.  It has basic information about the workers, for instance the first name, last name, salary and a method called print full name.  It's a basic console application that shows the meaning of inheritance.


Inheritance

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

namespace Inheritance
{
    public class Employee
    {
        public string FirstName;
        public string LastName;
        public string Email;

        public void PrintFullName()
        {
            Console.WriteLine(FirstName + " " + LastName);
        }

        public class FullTimeEmployee : Employee
        {
            public float YearlySalary;
        }
        public class PartTimeEmployee : Employee
        {
            public double HourlyRate;
        }
        public class A : PartTimeEmployee
        {
           
        }



        public class Program
        {
            static void Main(string[] args)
            {
                FullTimeEmployee FTE1 = new FullTimeEmployee();
                FTE1.FirstName = "Dave";
                FTE1.LastName = "Jones";
                FTE1.YearlySalary = 50000;
                FTE1.PrintFullName();

                FullTimeEmployee FTE2 = new FullTimeEmployee();
                FTE2.FirstName = "Pragim";
                FTE2.LastName = "Technologies";
                FTE2.YearlySalary = 50000;
                FTE2.PrintFullName();

                FullTimeEmployee FTE3 = new FullTimeEmployee();
                FTE3.FirstName = "Vinny";
                FTE3.LastName = "Baddacuci";
                FTE3.YearlySalary = 60000;
                FTE3.PrintFullName();

                PartTimeEmployee PTE1 = new PartTimeEmployee();
                PTE1.FirstName = "Lisa";
                PTE1.LastName = "Pattery";
                PTE1.HourlyRate = 15.50;
                PTE1.PrintFullName();

                PartTimeEmployee PTE2 = new PartTimeEmployee();
                PTE2.FirstName = "Ben";
                PTE2.LastName = "Caz";
                PTE2.HourlyRate = 15.50;
                PTE2.PrintFullName();

                A PTEA1 = new A();
                PTEA1.FirstName = "Jan";
                PTEA1.LastName = "Rubble";
                PTEA1.HourlyRate = 15.00;
                PTEA1.PrintFullName();




                Console.ReadLine();


            }
           
        }
    }
}