2/29/2024 0 Comments Book class polymorphism java code![]() Now, let’s move further with this article and understand different types of Polymorphism in Java. I hope you got an idea on the concept of Polymorphism. This makes Polymorphism in Java a very useful concept and it can be applied in real-world scenarios as well. ![]() Thus, Polymorphism increases the simplicity and readability of the code by reducing the complexity. But if you create YJeans class object, that extends ABCShoppingCenter class, then you can have both the variants. Now, which method gets invoked will depend on the type of object it belongs to. If you create ABC ShoppingCenter class object, then there will be only one jeans available. Thus, the method named jeans() has two definitions – one with only default jeans and other with both, the default jeans and the new variant. So, instead of creating different methods for every new variant, we can have a single method jeans (), which can be defined as per the different child classes. The YJeans class includes a method named jeans(), using which you can get both the jeans variants. Both the classes XJeans and YJeans extends the parent class ABCShoppingCenter. For the Jeans in the neighboring town, there is another class YJeans. ![]() Using this method, you can get an Allen Solly jeans. Now relating this concept to an object-oriented language like Java, suppose you have a class named XJeans which includes a method named jeans(). Why? Because that was a specialty of the shop that was located in the neighboring town. But you decided to buy it from the shop near to your home. Once back home, you again went to the Shopping Center near your home to get those amazing pair of Jeans but couldn’t find it. You walk into the shop and find a new variant of the same brand which you liked even more. A week later, while traveling to a nearby town, you spot another Shopping center. Suppose you went to a Shopping Centre (Allen Solly) near your home and bought a pair of jeans. Now let’s take another example and understand polymorphism in depth. This is also known as Method Overloading. But, if two contact numbers are passed to this method at the same time, then both will be saved under the same contact. ![]() If one parameter is passed, then only a single contact number is saved under the contact. Here, which definition is to be executed depends upon the number of parameters being passed. void createContact(String name, int number1).Īs you can see in the above figure, createContact() method has two different definitions. createContact(). But, instead of taking two contact numbers as parameters, take only one contact number as a parameter i.e. In such situations, instead of creating another method with a different name to save one number for a contact, what you can do is, create another method with the same name i.e. Few of them might be having only a single contact number. Now, it’s not necessary that everyone in your contact list will have two contact numbers. S uppose you want to write a function to save two contact numbers of the same person, you can create it like – void createContact(String name, int number1, int number2). Similarly, in Java, an object is only one but it can take multiple forms depending on the context of the program. For the ease of accessibility, your cellphone provides you the functionality where you can save two numbers under the same name. Suppose a person has two contact numbers. Consider a cell phone where you save your Contacts.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |