![]() ![]() If AreaOfTank required an extra field, then the field should be unique from the called fields like Color.īy 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. So there is no need for AreaOfTank class to initialize these values. That means Tank() constructor is called and it will initialize the value of Radius and Height in AreaOfTank(). Here AreaOfTank() call base class constructor with the parameter r and h. Public AreaOfTank(string c, double r, double h) : base (r, h) But we declare the constructor of AreaOfTank with a base keyword as shown here: Both the base class and the derived class have their own constructor. The Tank class describes the dimension of the tank and AreaOfTank describe the color and the area of the tank. The radius of tank is :4 and the height of tank is :8Įxplanation: In the above example, Tank is the base class and AreaOfTank is the derived class. Here, argument-list contains arguments that are required by the constructor of the base class. 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.ĭerived-constructor(parameter-list) : base(argument-list) With the help of base keyword, the derived class can call the constructor which is defined in its base class. To overcome this situation C# provide a keyword known as a base keyword. 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.Ĭase 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. Tank class provides the dimensions of the tank and AreaOfTank provides the color and the area of the tank. The radius of tank is :6 and the height of tank is :12Įxplanation: In the above example Tank is the base class and AreaOfTank is the derived class. Different ways to sort an array in descending order in C#.String.Split() Method in C# with Examples. ![]() C# | How to check whether a List contains a specified element.Difference between Abstract Class and Interface in C#.C# | How to use strings in switch statement.C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |