Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active August 29, 2015 14:17
Show Gist options
  • Save dnasca/f6ab02bc4e531bffc811 to your computer and use it in GitHub Desktop.
Save dnasca/f6ab02bc4e531bffc811 to your computer and use it in GitHub Desktop.
using System;
using System.Dynamic;
/* Basic Inheritance
*
* C# Supports only single class inheritance
* Multi-level class inheritance is possible
* C# supports multiple interface inheritance
* The Child class is a specialization of the base class
* Base classes are automatically instantiated before the derived classes
* The Parent Class constructor executes before the Child Class constructor
*
*/
namespace BasicInheritance
{
public class Employee
{
//public fields are BAD PRACTICE. good practice involves exposing private fields to the public using properties
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 float HourlyRate;
}
class Program
{
static void Main(string[] args)
{
//initilizing with assignment statements
FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(); //explicit
fullTimeEmployee.FirstName = "Salary";
fullTimeEmployee.LastName = "Man";
fullTimeEmployee.YearlySalary = 5000000;
fullTimeEmployee.PrintFullNAme();
//initilizing with object initilizer
var partTimeEmployee = new PartTimeEmployee {FirstName = "Wage", LastName = "Earner", HourlyRate = 25}; //more implicit-- var can be used to for initilizations, and the nice and neat shorthand
partTimeEmployee.PrintFullNAme();
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment