04-17.eri-test Some people know nothing about polymorphism

This article was originally published on my blog:
https://pleasefindencoded.blogspot.com/2020/04/post-79-what-some-people-dont-know.html

In the context of programming, polymorphism means-in an abstract sense-when programming constructs or parts of code, such as classes or methods, can be applied to multiple objects of different types.

Usually, this is the end of most people's knowledge. Most people do not know that there are different kinds of polymorphism. In this article, I will explain the different types of polymorphism and discuss their respective advantages and disadvantages:

Subtype-Polymorphism

If people talk about polymorphism, it usually refers to this type of polymorphism. This is also the simplest polymorphism. When subtyping, objects of the subtype can be used anywhere the parent type is used. E.g:

类Supertype {} 类子类型扩展超类型{} ... 超型sup1 =新的Supertype(); 超型sup2 =新的Subtype();

In the above example, subtypes can be used when supertypes are used.

another example:

class MyObject1 {}
class MyObject2 {}

MyObject1 o1 = new MyObject1();
MyObject2 o2 = new MyObject2();
List和lt;Object> obList = new ArrayList<>();
obList.add(o1);
obList.add(o2);
obList.add(2);
obList.add("hello");

In the above example, obListdifferent types of objects can be used-from custom objects to numbers to strings.

advantage

  • Subtype polymorphism gives you flexibility

Disadvantages

  • It is difficult to limit flexibility
  • You will lose type safety because you can only control which objects are allowed to be added to the container, and if you take an object out of the container, you must cast it to the original type in order to use it in meaningful situations Method

Parametic Polymorphism

Sometimes you want to reuse a class for different types, but you do n’t want to write your own class for each type. So basically, you just want to change certain parameters to specific types. This is parameter polymorphism.

When creating a class, set the generic type Ť. In a class, you think of a generic type as any other type. At initialization, the general type T is designated as a specific type. E.g:

public class MyObject1<T> {
    T obj;    
    void setObj(T obj) {
        this.obj = obj;
    }

    T getObj() {
        return this.obj;
    }
}

Suppose you only want to apply this class to Integers, and then initialize it as follows:

MyObject1<Integer> object1 = new MyObject1<Integer>();

On another instance, you want to reuse the same class, but for another type, you can initialize it with a different class:

MyObject1<String> object2 = new MyObject1<String>();

You can use this technique not only for one type, but also for multiple types. E.g:

public class KeyValue<K, V> {
    private K key;    
    private V value;        
    void setKeyValue(K key, V value) {
        this.key = key;
        this.value = value;
    }

    K getKey() {
        return this.key;
    }

    V getValue() {
        return this.value;
    }
}

advantage

  • Usually perform more powerful type analysis at compile time

Disadvantages

  • Compared to subtype polymorphism, parametric polymorphism provides less flexibility
  • General parameters do not know the properties and methods of the type to be applied last

Limited parametric polymorphism

Limited parametric polymorphism is based on parametric polymorphism. By defining an upper limit for types, you can limit type parameters to specific types. For example, this is useful when you want to build different types of containers and need to know the existence of certain properties or methods in advance.

For example, you have a basket of different foods and want to know the oldest of these elements. Using limited parametric polymorphism, you can achieve this:

公共接口老化{
     日期getBestBeforeDate();
 }
public class FoodBasket<T 延伸 Ageable> {
    private T oldestFood;
    public void put(T food) {
        if (oldestFood == null ||
food.getBestBeforeDate().compareTo(oldestFood.getBestBeforeDate()) < 0) {
            oldestFood = food;
        }
    }

    public T getOldestFood() {
        return oldestFood;
    }
}

By using extendswe are creating an upper limit for our type. Therefore, we know that our type must have the properties or methods defined in the superclass resp. Interface and can use them.

Suppose you want to use the basket for a specific kind of food and want to extend it with other functions, then you can do this:

公共类Egg实现了Ageable {
     私人日期bestBeforeDate;
     鸡蛋(Date bestBeforeDate){
         this.bestBeforeDate = bestBeforeDate;
     }

     @Override
     公开日期getBestBeforeDate(){
         返回this.bestBeforeDate;
     }
 }
public class OldEggsBasket extends FoodBasket<Egg> {
    // additional functions
}

Types can also implement multiple interfaces. The interfaces are separated by ampersands &:

公共接口可打印{
     无效print();
 }
public class FoodBasket<T extends Ageable & Printable> {
    private T oldestFood;
    public void put(T food) {
        if (oldestFood == null ||
 food.getBestBeforeDate().compareTo(oldestFood.getBestBeforeDate()) < 0) {
            oldestFood = food;
        }
        food.print();
    }
    //...
 }

advantage

  • Write reusable code, such as library classes, for other programmers to use, they can parameterize as needed

Disadvantages

  • Less flexible than the above polymorphism

Ad-hoc Polymorphism

Most developers may already know this concept under a different name: overloading. With overloading, you will provide different methods with the same name for different signatures. The most common one is the plus (+) operator.

advantage

  • (Overload) Method naming flexibility

Disadvantages

  • No code reuse. Therefore, unlike other types of polymorphism, it is not really considered polymorphic

I am interested in whether you know more advantages and disadvantages of each polymorphic type.

from: https://dev.to//pandaquests/what-some-people-don-t-know-about-polymorphism-3eok

Published 0 original articles · liked 0 · visits 124

Guess you like

Origin blog.csdn.net/cunbang3337/article/details/105583710