When you have inheritance between two or more classes. One is base classs (parent class). Another is derived class (child class). You want to access the members such as fields, properties, and methods, within a derived class. In this type of situation you can use base keyword in derived class to access members of base class.
When you want to explicitly call a constructor of the base class from within a constructor of the derived class. You can write the code with base keyword before the call of the constructor of the derived call. It will execute the base classs constructor before the execution of the derived class constructor.
In below example, we have Fruit (parent class) which have a constructor with name Fruit(string name). We have inherited the Fruit (parent class) with Apple (chile class). We also have a Apple(string name) child class constructor that is inheirted with base class constructor with : base(name) keword.
// Base class (parent class)
// Base class constructor.
// Derive class (child class)
// Derived class constructor.
// Base class (parent class)
// Base class constructor.
// Derive class (child class)
// Derived class constructor.
You can use the base keyword to access and call members of the base class, even if those members are hidden or overridden in the derived class. In below example, We have Fruit (parent class) that have Color field. We have inherited child class Apple with Fruit parent class and with base keword we are accessing parent class member (Color) in child Apple class.
// Base class (parent class)
// Derive class (child class)
// Base class field
// Derived class field.
// Base class (parent class)
// Derive class (child class)
// Base class field
// Derived class field.
//Child class instance
// Base class (parent class)
// Base class field.
// Base class constructor.
// Base class method.
// Base class method code
// Derive class (child class)
// Derive class constructor.
// Derive class method.
//Child class instance
// Base class (parent class)
// Base class field.
// Base class constructor.
// Base class method.
// Base class method code
// Derive class (child class)
// Derive class constructor.
// Derive class method.
Here's a basic breakdown of how Fruit class is working in below exmaple: | |
public class Fruit | Here, Fruit is parent class that is public which mean this class is accessible outside the class. |
public string Color = "Red"; | Here, Fruit class has Color data member which is public that mean it is accessible outside the class and it's data type is string. |
public Fruit() { Console.WriteLine("Fruit name is Apple and variety is RedDelicious"); } |
Here, Fruit() is constructor of Fruit class |
public virtual void getFruit(string fruitName) { // Base class method code Console.WriteLine("Fruit name is : {0} and it's color is :{1}", fruitName, Color); } |
Here, getFruit(string fruitName) is virtual method that accessible in Fruit parent class. |
Here's a basic breakdown of how Apple subclass is working in below exmaple: | |
public class Apple: Fruit | Here, Apple is child class of Fruit that is inherited with Fruit parent class. |
public Apple() : base() { // Derived class constructor code Console.WriteLine("RedDelicious apple has sweet, tart eating taste"); } |
Here, Apple() is child class constructor that is inherited with parent class constructor with : base keword. Here base class constructor will call first then child class constructor will call. |
public override void getFruit(string fruitName) { base.getFruit("Apple"); Console.WriteLine("{0} has sweet, tart eating taste and it's color is {1}", fruitName, base.Color); } |
Here, getFruit(string fruitName) is a method that is overridden in Apple child class from Fruit base class and with base keword we are calling Fruit class method getFruit("Apple"); |
You can access the members of base class with derive classs by using base keyword with derived class. When you want to work with members from the base class, even if those members are hidden or overridden in the derived class, You can use base keyword.