Gyh's Braindump

Java Polymorphism

tags
Java, Polymorphism
source
stackify-polymorphism-in-java
source
DT-On Java 8

Static Polymorphism

Or compile time.

Java Method Overloading

Dynamic Polymorphism

A derived class override methods(same name and parameters) of its base class.

One advantage is that when types belongs to a general type needed to be operated by the same way. Objects can defined as the general type instead of specific types, so when a new type is added, the operation code doesn’t need to change.

Using a general type to operate derived types is called Upcasting.

void doSomething(Shape shape) {
    shape.erase();
    // ...
    shape.draw();
}

Circle circle = new Circle();
Triangle tri = new Triangle();
doSomething(circle);
doSomething(tri);

Fields are not polymorphic

positioneaseboxintervaldue
front2.5036.002021-01-28T20:26:18Z

When derived class instance upcasting to base class instance, fields would be base class fields.

class Super {
  public int field = 0;
  public int getField() { return field; }
}

class Sub extends Super {
  public int field = 1;
  @Override
  public int getField() { return field; }
  public int getSuperField() { return super.field; }
}

public class FieldAccess {
  public static void main(String[] args) {
    Super sup = new Sub(); // Upcast
    System.out.println("sup.field = " + sup.field +
      ", sup.getField() = " + sup.getField());
    Sub sub = new Sub();
    System.out.println("sub.field = " +
      sub.field + ", sub.getField() = " +
      sub.getField() +
      ", sub.getSuperField() = " +
      sub.getSuperField());
  }
}
/* Output:
sup.field = 0, sup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField()
= 0
*/

Static methods are not polymorphic

positioneaseboxintervaldue
front2.5021.002021-01-23T20:25:17Z

#static

class StaticSuper {
  public static String staticGet() {
    return "Base staticGet()";
  }
  public String dynamicGet() {
    return "Base dynamicGet()";
  }
}

class StaticSub extends StaticSuper {
  public static String staticGet() {
    return "Derived staticGet()";
  }
  @Override
  public String dynamicGet() {
    return "Derived dynamicGet()";
  }
}

public class StaticPolymorphism {
  public static void main(String[] args) {
    StaticSuper sup = new StaticSub(); // Upcast
    System.out.println(sup.staticGet());
    System.out.println(sup.dynamicGet());
  }
}
/* Output:
Base staticGet()
Derived dynamicGet()
*/

Covariant Return Types

positioneaseboxintervaldue
front2.5010.012021-01-23T15:07:17Z

#covariant

an overridden method in a Derived Class can return a type dertived from the type returned by the base-class method.

class Grain {
  @Override
  public String toString() { return "Grain"; }
}

class Wheat extends Grain {
  @Override
  public String toString() { return "Wheat"; }
}

class Mill {
  Grain process() { return new Grain(); }
}

class WheatMill extends Mill {
  @Override
  Wheat process() { return new Wheat(); }
}

public class CovariantReturn {
  public static void main(String[] args) {
    Mill m = new Mill();
    Grain g = m.process();
    System.out.println(g);
    m = new WheatMill();
    g = m.process();
    System.out.println(g);
  }
}
/* Output:
Grain
Wheat
*/

Links to this note