![]() We then invoke the displayAccountType() method, which runs displayAccountType() from the SavingsAccount method. In our main program, we first declare an instance of SavingsAccount called johnSavings. So, instead of executing the displayAccountType() method in the SavingsAccount class, our program executes the displayAccountType() method in the BankAccount class. When displayParentAccountType() is executed, we use the super keyword to invoke the displayAccountType() method from our parent class. When displayAccountType() is executed, Account type: Savings Account is printed to the console. We have also declared a subclass called SavingsAccount which has two methods: displayAccountType() and displayParentAccountType(). When this method is invoked, Account type: Bank Account is printed to the console. In this example, we have declared a superclass called BankAccount which has one method: displayAccountType(). Here’s an example of inheritance being used to inherit the code stored in a BankAccount method for use in a new method called SavingsAccount: Here are a few examples of this type of relationship:īy inheriting code from another method, you can access the attributes, constructors, and methods stored in that function and use those in your new method. Inheritance is used when there is an is-a relationship between two classes. ![]() Inheritance is a useful feature because it allows coders to reduce the amount of code they have to repeat because they can use existing code to create new classes. In Java, inheritance is used to define a new class from an existing one. By the end of reading this tutorial, you’ll be a master at using the super keyword in Java. This tutorial will discuss, with references and examples, the basics of the Java super keyword and how you can use it in your code. Access exclusive scholarships and prep coursesīy continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.Career Karma matches you with top tech bootcamps.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |