<< Chapter < Page Chapter >> Page >

Programs were fairly long

However, even though those programs were simple in concept, they were relatively long. That made them somewhat difficult to explain due simply to the amount of code involved.

Keep it short and simple

Beginning with this module, I am going to back away from real-world scenarios and begin using sample programs that are as short and as simple as I know how to make them, while still illustrating the important points under discussion.

My objective in this and future modules is to make the polymorphic concepts as clear as possible without having those concepts clouded by other programming issues.

I will simply ask you to trust me when I tell you that polymorphism has enormous applicability in the real world.

A little more on inheritance

There is another aspect of inheritance that I didn't explain in the previous modules.

Every class extends some other class

Every class in Java extends some other class. If you don't explicitly specify the class that your new class extends, it will automatically extend the class named Object .

A class hierarchy

Thus, all classes in Java exist in a class hierarchy where the class named Object forms the root of the hierarchy.

Some classes extend Object directly, while other classes are subclasses of Object further down the hierarchy.

Methods in the Object class

The class named Object defines default versions of the following methods:

  • clone()
  • equals(Object obj)
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
  • wait(long timeout)
  • wait(long timeout, int nanos)

As you can see, this list includes three overloaded versions of the method named wait . The three versions have the same name but different formal argument lists. Thus, these three methods are overloaded versions of the method name wait .

Every class inherits these eleven methods

Because every class is either a direct or indirect subclass of Object , every class in Java, (including new classes that you define) , inherit these eleven methods.

To be overridden ...

Some of these eleven methods are intended to be overridden for various purposes. However, some of them, such as getClass , notify , and the three versions of wait , are intended to be used directly without overriding. (Although not shown here, these five methods are declared to be final , meaning that they may not be overridden.)

What is polymorphism?

The meaning of the word polymorphism is something like one name, many forms.

How does Java implement polymorphism?

Polymorphism manifests itself in Java in the form of multiple methods having the same name.

In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods) . In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods) .

Three distinct forms of polymorphism

From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java:

  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface

Method overloading

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask