![]() ![]() So what I mean by that is so when reference object or reference of a parent class points to the object of the sub-class it’s called upcasting. Now there is a property in Java called polymorphism by which you can define the reference or you can point the reference to a base class to any object of the derived class. ![]() The main bank itself has 0, Bank ABC has 5% interest rate, Bank DEF has 6% interest rate and Bank XYZ has 10 % interest rate. So I have the same method in all the four classes get interest rate and the only difference in this function is the rate of interest. ![]() So this is the derived class and this is the base class also have same method get interest rate and the interest rate in this bank is 10%. Simple! package lesson1 Īnd Bank class XYZ which is also extending from Bank. So I have defined a method here which returns 6%. package lesson1 īank_DEF, which also extends from Bank class have the rate of interest 6%. And this also contains the same method but this returns the rate of interest or this bank has a rate of interest = 5%. Second, class is called Bank_ABC, which extends from bank class because bank class is our base class and Bank_ABC is our derived class. So one is called the bank class which will be my main class. So this is how I have created these classes. So if you don’t know how to make a class, just right click new, go to class, and in here name the class. We also have defined Bank_ABC class, Bank_DEF class and Bank_XYZ class. And because it’s a base class It will be returning 0 as an interest. This base class method just gives the rate of interest so we will define this method as get interest rate. One is called the bank class, which will be my base class and this bank class contain three or one methods. How we can make our classes polymorphism or how you can define an object which follows polymorphism. So polymorphism literally means the word polymorphism means having different forms and whenever you define an object, class object I should say and its property and quality having many forms is called polymorphism. Polymorphism is the ability of an object to have a different form. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |