This is a simple example of upcasting, wherein the parent class refers to an object from the child class. By using upcasting, you can access the child version of the parent class. This code example defined a variable m圜ar of type Car and used it to hold a new object - BMW. Then, it used the same variable to refer to Mercedes, which is possible because they are both child classes of Car. Therefore, you can use the parent class, Car, to refer to either of the two types of cars. With polymorphism, you can assume that the Car (parent class) can be a BMW (child class) or a Mercedes (child class). This means they inherit its attributes and functions. If you have two kinds of cars - for example, BMW and Mercedes - you can create a Car class, then have a BMW class and a Mercedes class that each inherit from the Car class. Instead, you should extend the functionality by means of inheritance and polymorphism. In other words, when you try to add more functionality to your code, you shouldn’t modify the existing classes (closed for modification). Polymorphism is also used to support the “Open-Closed” principle, which represents the “O” in the SOLID acronym. It states that code should be open for extension and closed for modification. Alternatively, it allows a function with the same name to be implemented in several different ways and will select one to execute based on the number and types of parameters given. In contrast, polymorphism allows for a child class to define its version of a function with the same name as one in its parent class. For example, inheritance enables you to use the attributes and methods of a parent class. Inheritance is a way to enable code reusability while polymorphism is a way to dynamically decide which version of a function will be invoked. With polymorphism, the child class inherits the attributes and methods, but provides its own implementation (code) for these methods. In inheritance without polymorphism, the child class inherits the same attributes and methods of the parent class without any modifications to their functionality. methods of Shape class are not specified here but are automatically accessible to this Square class thanks to inheritance In inheritance, a class that’s referred to as a child class can inherit the methods and attributes from another class (the parent class). See the following example: However, the two principles are substantially different. Understandably, developers sometimes confuse polymorphism with another core concept of OOP: inheritance. Differences Between Inheritance and Polymorphism Additionally, note that the output depends on the type of the passed parameters. You’ll notice that while calling the two methods, there’s no difference except for the parameters passed. When the code is run, the output is as follows:ĭouble Multiplication, Result = 17.849999999999998 Polymorphism explores how to create and use two methods with the same name to execute two different functionalities - like adding two functions with the same name but that accept different parameters.įor example, let’s explore how you can define two functions with the name Multiply (). One is used to calculate the product of two integers, and the other is used to calculate the product of two doubles. Polymorphism, which literally means “different forms,” is one of the core concepts of OOP.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |