Inheritance
What exactly is Inheritance? MSDN explanation says : Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.
So what can we inherit? For example we have Car and Motorcycle classes, and as we know cars and motorcycles have a few things in common such as engine and color. We could create a base class named Vehicle that has those properties and make Car and Motorcycle classes which will inherit Vehicle class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
using System; namespace Vehicle { public class Vehicle { public string EngineModel { get; set; } public string VechileColor { get; set; } } public class Car : Vehicle { public bool IsHybrid { get; set; } } public class RacingCar :Car { public bool IsStreetLegal { get; set; } } public class Motorcycle : Vehicle { public bool HasSideCar { get; set; } } public class Program1 { private static void Main(string[] args) { Car car = new Car(); car.EngineModel = "Medium Engine Mk.1"; car.VechileColor = "Black"; car.IsHybrid = false; // Try commenting next line and you will see proof that Car class can inherit from Vehicles class not from RacingCar class // or that //car.IsStreetLegal = true; Motorcycle motorcycle = new Motorcycle(); motorcycle.EngineModel = "Light Engine Mk. 4"; motorcycle.VechileColor = "Orange"; // Try uncommenting next line to see if Motorcycle class can iherit from Car class //motorcycle.IsHybrid = true; RacingCar raceCar = new RacingCar(); raceCar.EngineModel = "Lightning Engine Mk. 7"; raceCar.VechileColor = "Red"; raceCar.IsStreetLegal = true; car.IsHybrid = false; Console.WriteLine("Car engine model is {0} and color of a car is {1}. Is car hybrid? {2}", car.EngineModel, car.VechileColor, car.IsHybrid); Console.WriteLine("Motorcycle engine model is {0} and color of a motorcycle is {1}. Does this model come with side car? {2}", motorcycle.EngineModel, motorcycle.VechileColor, motorcycle.HasSideCar); Console.WriteLine("Racing car engine model is {0} and color of a racing car is {1}. Is racing car hybrid? {2}. Is racing car legal to drive on the streets? {3}", raceCar.EngineModel, raceCar.VechileColor, raceCar.IsHybrid, raceCar.IsStreetLegal); } } } |
Can i inherit from multiple classes? No, but you can Inherit multiple Interfaces that inherit one class.
I don’t wish for my class or method to be directly instantiated. What can I do about that problem? You can make those classes or methods abstract. Beware, when you make your class abstract one, none of your class members have to be marked as abstract, but if at least one of your members is marked as abstract, entire class must be marked as abstract.
Can derived class access private fields of base class? No, it can not. You can only access internal, public, protected, and protected internal members of a base class.
Is it possible to prevent our class from becoming other classes base class? Yes, mark that class as sealed and other classes cannot inherit from this class.