HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <h2>Creating objects</h2>
1 <h2>Creating objects</h2>
2 <p>Look at the function below and say if it is polymorphic or not:</p>
2 <p>Look at the function below and say if it is polymorphic or not:</p>
3 <p>On the one hand, it is polymorphic because we pass the user from outside, and we can substitute it by passing an object of another class there. On the other hand, we use the class<em>EmailSender</em>explicitly inside the function, and you can't replace it without rewriting the code itself.</p>
3 <p>On the one hand, it is polymorphic because we pass the user from outside, and we can substitute it by passing an object of another class there. On the other hand, we use the class<em>EmailSender</em>explicitly inside the function, and you can't replace it without rewriting the code itself.</p>
4 <p>This code demonstrates a simple but crucial idea. Subtype polymorphism is possible when we pass an object into a function from outside rather than construct directly inside it.</p>
4 <p>This code demonstrates a simple but crucial idea. Subtype polymorphism is possible when we pass an object into a function from outside rather than construct directly inside it.</p>
5 <p>We can also create an object inside a function, but we should obtain the class name dynamically. We will look at this technique later in the lesson about metaprogramming.</p>
5 <p>We can also create an object inside a function, but we should obtain the class name dynamically. We will look at this technique later in the lesson about metaprogramming.</p>
6 <h2>Checking types</h2>
6 <h2>Checking types</h2>
7 <p>Another example is with a catch. Is there polymorphism in the code below?</p>
7 <p>Another example is with a catch. Is there polymorphism in the code below?</p>
8 <p>Everything seems fine in this example: we pass the object from outside, but there is one snag. We check the type explicitly inside the function. So that behavior is not determined by the type.</p>
8 <p>Everything seems fine in this example: we pass the object from outside, but there is one snag. We check the type explicitly inside the function. So that behavior is not determined by the type.</p>
9 <p>The function itself decides how to behave. Moreover, it is hardwired to the types defined within it. If they change, we have to rewrite the function. As a result, there is no subtype polymorphism.</p>
9 <p>The function itself decides how to behave. Moreover, it is hardwired to the types defined within it. If they change, we have to rewrite the function. As a result, there is no subtype polymorphism.</p>
10 <p>Type checking sometimes occurs and can be used to keep the code simple, but more often, it's a sign of poor design. One might say that such code doesn't correspond to OOP in its modern sense.</p>
10 <p>Type checking sometimes occurs and can be used to keep the code simple, but more often, it's a sign of poor design. One might say that such code doesn't correspond to OOP in its modern sense.</p>
11 <p>There are several approaches to solving the problem above:</p>
11 <p>There are several approaches to solving the problem above:</p>
12 <ul><li><p>Transferring the logic inside the classes. Then the function code becomes: user.sayHi(). You should be careful with this approach because it's easy to end up with<a>god object</a>. Generally, it's better to take a different approach.</p>
12 <ul><li><p>Transferring the logic inside the classes. Then the function code becomes: user.sayHi(). You should be careful with this approach because it's easy to end up with<a>god object</a>. Generally, it's better to take a different approach.</p>
13 </li>
13 </li>
14 <li><p>Adding a new interface in the form of the isUser and isGuest methods</p>
14 <li><p>Adding a new interface in the form of the isUser and isGuest methods</p>
15 </li>
15 </li>
16 </ul><p>Although there's no less code, it's still subtype polymorphism. The code is bound to methods, not types. Changing the class structure will not affect this function if the logic remains the same.</p>
16 </ul><p>Although there's no less code, it's still subtype polymorphism. The code is bound to methods, not types. Changing the class structure will not affect this function if the logic remains the same.</p>