Will incur an interest charge each month where the end of month balance isn't 0.Can have a negative balance, but not be greater in absolute value than the credit limit.Will get a credit of 2% of the month-ending-balance.The requirements for the new classes can be stated as follows:
#Object programming language list code
Once you've updated the constructors, you can develop the code for each of the derived classes. Some classes define multiple constructors, and this syntax enables you to pick which base class constructor you call. You use the : base() syntax to indicate a call to a base class constructor. The parameters to this new constructor match the parameter type and names of the base class constructor. The following code shows the constructor for the InterestEarningAccount: public InterestEarningAccount(string name, decimal initialBalance) : base(name, initialBalance) You declare a constructor that can pass arguments to the base class constructor. That means each derived class must explicitly call this constructor. The compiler doesn't generate a default constructor when you define a constructor yourself. The BankAccount class declares one public constructor with the following signature: public BankAccount(string name, decimal initialBalance)
The proper initialization normally happens without any extra code.
#Object programming language list how to
A derived class constructor must initialize the derived class, and provide instructions on how to initialize the base class object included in the derived class. A constructor is responsible for initializing an object.
When you create the classes as shown in the preceding sample, you'll find that none of your derived classes compile. In either tool, name the file to match the class: InterestEarningAccount.cs, LineOfCreditAccount.cs, and GiftCardAccount.cs. In Visual Studio Code, select File then New to create a new source file. In Visual Studio, you can right-click on the project, and select add class to add a new class in a new file. It's a good practice to create each new class in a different source file. These derived classes already have all the behavior defined in the BankAccount class. Write the implementations for new and different functionality in each of the derived classes. Public class GiftCardAccount : BankAccountĮach of these classes inherits the shared behavior from their shared base class, the BankAccount class. Public class LineOfCreditAccount : BankAccount These new classes can extend the BankAccount class with the specific behavior needed for each type: public class InterestEarningAccount : BankAccount Instead, you can create new bank account types that inherit methods and data from the BankAccount class created in the preceding tutorial. Any changes would be copied across all the affected classes. That technique would work in the short term, but it would be more work over time. You could copy that code, rename the classes, and make modifications. It can be refilled once at the start of each month.Īll of these different accounts are similar to BankAccount class defined in the earlier tutorial. A pre-paid gift card account that starts with a single deposit, and only can be paid off.A line of credit that can have a negative balance, but when there's a balance, there's an interest charge each month.An interest earning account that accrues interest at the end of each month.Over time, needs change, and related account types are requested: It works great in the situation where there is only one bank account type. Create different types of accountsĪfter building this program, you get requests to add features to it. You'll also add features to the BankAccount class, taking advantage of the abstraction and encapsulation techniques you learned in the preceding tutorial. In this tutorial, you'll extend that application to make use of inheritance and polymorphism to add new features. Both the BankAccount and Transaction classes provide encapsulation of the components needed to describe those concepts in code. You could modify its implementation without affecting any of the code that used the BankAccount class. The BankAccount class provided an abstraction for the concept of a bank account. In the preceding tutorial, introduction to classes you saw both abstraction and encapsulation. Polymorphism Ability to implement inherited properties or methods in different ways across multiple abstractions.Inheritance Ability to create new abstractions based on existing abstractions.Encapsulation Hiding the internal state and functionality of an object and only allowing access through a public set of functions.Abstraction Modeling the relevant attributes and interactions of entities as classes to define an abstract representation of a system.The four basic principles of object-oriented programming are: C# is an object-oriented programming language.