OnJava8-Examples/polymorphism/StaticPolymorphism.java

34 lines
761 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: polymorphism/StaticPolymorphism.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Static methods are not polymorphic.
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()";
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String dynamicGet() {
return "Derived dynamicGet()";
}
}
public class StaticPolymorphism {
public static void main(String[] args) {
StaticSuper sup = new StaticSub(); // Upcast
2015-05-06 12:09:38 -07:00
System.out.println(StaticSuper.staticGet());
2015-04-20 15:36:01 -07:00
System.out.println(sup.dynamicGet());
}
} /* Output:
Base staticGet()
Derived dynamicGet()
*///:~