Base Keyword in C Sharp– Examples for all Uses

base keyword in c# with program examples for all concepts where base keyword can be used. For example invoking constructor, method and variables etc. of base class from derived class in inheritance relationship.

base keyword in c# programs can be used for following concepts.

  • Calling base class constructor from derived class.
  • Calling immediate parent class / base class method.
  • Initialize a base class variable from derived class.

NOTE:
Base class / parent class all are terminology for base class.
Derived class/ child class all are terminology for derived class.

1)Call base class constructor from derived class using super keyword in C#

We use base keyword in C# program to call base class constructor for derived class. You can see the example how base keyword has been used in derived class constructor to invoke base class constructor.

Important point: If the base class constructor has no parameter, then we don’t need to call base() in derived class constructor and compiler will call it implicitly.

However, if the base class constructor has parameters then you must use base keyword with parameter.

A) Calling base class constructor with no parameter from derived class using base

c# code:

/*-------------------------------------------------
 * Example - Calling base class constructor
 */
class Parent {
 
	// Constructor
	public Parent() {
		Console.WriteLine("Parent class constructor");
	}
}
 
class Child : Parent {
 
	public Child()  {
 
		// Super call base class constructor
 
		// This is option for empty parameter
		// Even though you don't write it base class
		// constructor will be automatically called.


        Console.WriteLine("Child class constructor");
	}
}
 
public class Sample {
 
	public static void Main(String[] args) {
 
		// Create the object of child
		// base class constructor will also be called
		Child ch = new Child();
 
	}
 
}

Output:

Parent class constructor
Child class constructor

B) Calling parameterized constructor of base class from derived class using base

 /*-------------------------------------------------
 * Example - Calling base class constructor
 */
 
class Parent {	
	// Constructor
	public Parent(int age) {
	
		Console.WriteLine("Parent class constructor" + "Age :"+age);
	}
}
 
class Child : Parent {
 
	public Child():base(40) {
 
		//invoke base class constructor with parameter
		//ass the parent age

        Console.WriteLine("Child class constructor");
	}
}
 
public class Sample {
 
	public static void Main(String[] args) {
 
		// Create the object of child
		// base class constructor will also be called
		Child ch = new Child();
 
	}
 
}

Output:
Parent class constructor Age :40
Child class constructor

2)Call Base class method from Derived class using base Keyword

You can call a base class public method from a derived class using base keyword in C# programs.

For example, consider the Parent and Child class both have mobile () methods. When you create object of child class and call mobile () method, then child class
mobile method will be called. Because, Child class extends parent class and has overridden mobile method. You can refer method overriding example in C#
programs.

So, If we want to call parent class mobile method from child class method, then we need to call the parent’s class method using base keyword.

In below C# code, mobile method of parent class has been called using base keyword in child class mobile method.

/*-------------------------------------------------
 * Example - Calling base class method from derived class method.
 */
 
class Parent {
 
	public void mobile() {
		Console.WriteLine("Parent's mobile");
	}
}
 
class Child : Parent {
 
	public void mobile() {
 
		// Call parent's mobile() method		
		base.mobile();

        Console.WriteLine("Child's mobile");
 
	}
}
 
public class Sample {
 
	public static void Main(String[] args) {
 
		// Create the object of child
		Child ch = new Child();
		ch.mobile();// class child class method
 
	}
 
}

Output:
Parent’s mobile
Child’s mobile

3) Access base class public variables from derived class using base keyword

Lets say we a common field e.g. public int account is in both class parent and child. Child class extends base class. So base call variable is also accessible to child

class. But, when you print the account variable in child class, then child class variable will be printed not base class.

So, using bae keyword, we can refer base class variable from child class method.

  /*-------------------------------------------------
 * Example - Access base class variable from derived class
 */
 
class Parent {
 
	public int account = 10000;
 
	public void mobile() {
	}
}
 
class Child : Parent {
 
	public new int account = 5000;
 
	public new void account1() {
 
		// Show parent account
		Console.WriteLine("Parent's Account: " + base.account);
 
		// show child account
        Console.WriteLine("Child Account: " +account);
 
	}
}
 
public class Sample {
 
	public static void Main(String[] args) {
 
		// Create the object of child
		Child ch = new Child();
		ch.account1();
 
	}
 
}

Output:
Parent’s Account: 10000
Child Account: 5000

Related Posts