Polymorphism and inheritance in Java

Challenge yourself, with this brain-teasing introduction to Java method invocation in polymorphic method calls.

1 2 Page 2
Page 2 of 2

Also remember that method overriding is not method overloading.

It’s impossible to override a method if the parameters are different. It is possible to change the return type of the overridden method if the return type is a subclass of the superclass method.

What to remember about polymorphism

  • The created instance will determine what method will be invoked when using polymorphism.
  • The @Override annotation obligates the programmer to use an overridden method; if not, there will be a compiler error.
  • Polymorphism can be used with normal classes, abstract classes, and interfaces.
  • Most design patterns depend on some form of polymorphism.
  • The only way to use a specific method in your polymorphic subclass is by using casting.
  • It’s possible to design a powerful structure in your code using polymorphism.
  • Run your tests. Doing this, you will be able to master this powerful concept!

Learn more about Java

This story, "Polymorphism and inheritance in Java" was originally published by JavaWorld.

Copyright © 2019 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform