Created
July 16, 2015 03:50
-
-
Save AFelipeTrujillo/f803c5d23416cebafe73 to your computer and use it in GitHub Desktop.
Understanding the Inheritance in C-Sharp (C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CSharpBeginner | |
{ | |
public class InheritanceClass | |
{ | |
public InheritanceClass () | |
{ | |
} | |
public static void Main (string[] args) | |
{ | |
Car myCar = new Car (); | |
myCar.Make = "BMW"; | |
myCar.Model = "7531"; | |
myCar.Color = "Black"; | |
myCar.Year = 2005; | |
Console.WriteLine (myCar.FormatMe()); | |
Truck myTruck = new Truck (); | |
myTruck.Make = "Ford"; | |
myTruck.Model = "F345"; | |
myTruck.Color = "Navy"; | |
myTruck.Year = 2006; | |
myTruck.TowingCapacity = 1200; | |
Console.WriteLine (myTruck.FormatMe ()); | |
} | |
} | |
//Abstrac class which generalizes a Vehicle | |
public abstract class Vehicle | |
{ | |
public string Make { get; set; } | |
public string Model { get; set; } | |
public int Year { get; set; } | |
public string Color { get; set; } | |
public abstract string FormatMe(); | |
} | |
public class Car : Vehicle | |
{ | |
public override string FormatMe() | |
{ | |
return String.Format ("{0} - {1} - {2} - {3}", | |
this.Make, | |
this.Model, | |
this.Year, | |
this.Color | |
); | |
} | |
} | |
//with sealed property, other class doesnt uses this class for Inheritance | |
sealed public class Truck : Vehicle { | |
public int TowingCapacity { get; set; } | |
public override string FormatMe () | |
{ | |
return String.Format ("{0} - {1} - {2} Towing Units", | |
this.Make, | |
this.Model, | |
this.TowingCapacity | |
); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment