how to call base class constructor in c#


calling inheritance constructors classes examples base using procedure bestprog demonstration figure keyword three case To overcome this situation C# provide a keyword known as a base keyword. For additional examples, see new, virtual, and override. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. generate link and share the link here. A base class access is permitted only in a constructor, an instance method, or an instance property accessor. For example, if you specify class ClassB : ClassA, the members of ClassA are accessed from ClassB, regardless of the base class of ClassA. The language specification is the definitive source for C# syntax and usage. If AreaOfTank required an extra field, then the field should be unique from the called fields like Color.By using the base keyword, it becomes easier to initialize the objects of the base class without any conflict and it also provides an authority to call the constructor of a base class from the derived class and also save the time of re-writing the codes. And Tank class does not contain any constructor so the default constructor is used to instantiate the object of class and AreaOfTank class contains AreaOfTank() constructor which instantiate the object of class. In C#, both the base class and the derived class can have their own constructor. acknowledge that you have read and understood our, GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, C# | Jump Statements (Break, Continue, Goto, Return and Throw), Difference between Method Overriding and Method Hiding in C#, Different ways to make Method Parameter Optional in C#, Difference between Ref and Out keywords in C#, C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch), C# | How to use strings in switch statement, Difference between Abstract Class and Interface in C#, C# | How to check whether a List contains a specified element, String.Split() Method in C# with Examples, Different ways to sort an array in descending order in C#. Case 2: In this case, both the base class and derived class has their own constructors, so the process is complicated because the constructors of both classes must be executed. This example shows how to specify the base-class constructor called when creating instances of a derived class. C# | Difference between Static Constructors and Non-Static Constructors, C# | Multiple inheritance using interfaces, C# Program to Demonstrate Abstract Class with Multiple-level Inheritance, C# Program to Demonstrate Interface Implementation with Multi-level Inheritance, C# Program to Demonstrate the Inheritance of Abstract Classes, C# Program to Implement Multiple-Inheritance using Abstract Class and Interface, Complete Interview Preparation- Self Paced Course, Data Structures & Algorithms- Self Paced Course. Specify which base-class constructor should be called when creating instances of the derived class. Come write articles for us and get featured, Learn and code with the best industry experts. A-143, 9th Floor, Sovereign Corporate Tower, We use cookies to ensure you have the best browsing experience on our website. Explanation: In the above example, Tank is the base class and AreaOfTank is the derived class. So the objects of the derived class are instantiated by that constructor and the objects of the base class are instantiated automatically by the default constructor. For more information, see the C# Language Specification. By using the base keyword, it is possible to call the Getinfo method on the base class, from within the derived class. The constructor of a base class used to instantiate the objects of the base class and the constructor of the derived class used to instantiate the object of the derived class. In this example, both the base class, Person, and the derived class, Employee, have a method named Getinfo. But we declare the constructor of AreaOfTank with a base keyword as shown here: Here AreaOfTank() call base class constructor with the parameter r and h. That means Tank() constructor is called and it will initialize the value of Radius and Height in AreaOfTank(). The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. By using our site, you In C#, when we are working with the constructor in inheritance there are two different cases arise as follows: Case 1: In this case, only derived class contains a constructor. It is an error to use the base keyword from within a static method. Tank class provides the dimensions of the tank and AreaOfTank provides the color and the area of the tank. With the help of base keyword, the derived class can call the constructor which is defined in its base class. In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class. Writing code in comment?

Note: Any form of the constructor defined in the base class can be called by the base keyword, but only that constructor executes that matches the arguments. Explanation: In the above example Tank is the base class and AreaOfTank is the derived class. Get access to ad-free content, doubt assistance and more! The Tank class describes the dimension of the tank and AreaOfTank describe the color and the area of the tank. Here, argument-list contains arguments that are required by the constructor of the base class. Please use ide.geeksforgeeks.org, The base class that is accessed is the base class specified in the class declaration. So there is no need for AreaOfTank class to initialize these values. Both the base class and the derived class have their own constructor. Instead of inheriting constructors by the derived class, it is only allowed to invoke the constructor of base class.