The following C# program will incorporate many different aspects of object oriented programming into one assignment. The idea is to create one base class and one subclass. Within both of these classes, a method called “DisplayInfo()” is created which will be overwritten for the child class. The three variables within the method are shown at the bottom. Different access modifiers are used to define the scope or range for each variable.
The following code snippet shows the parent class “Cars” with the three variables. The method DisplayInfo() is defined as a virtual method so it can be overridden in the Mercedes class. The Mercedes class inherits the properties from the Cars class. The “base” keyword is used to access the “Cars” class properties. It is important to note that at this point, the price variable cannot be seen within the Mercedes class, so a property must be initialized.
The following code allows the price variable to be seen by the child class.
The following code displays the original variables initialized in the base class, then resets the variables to new values and displays those. The “price” variable is showing an error because the property “Price” must be used in its place.