Polymorphism in Java

Github

UPDATE: You can now clone or download BlueJ project from github. https://github.com/victorskl/demo-java

Polymorphism in OOP

Polymorphism is kind of powerful concept in Object-oriented programming. The following are quick overview.

  • simply means many forms
  • object variable reference
  • polymorphic value, i.e. referencing many
  • underlying concept is computer memory storage i.e. how computer program store variable and referencing or pointing at

A comprehensive explanation can refer to Wikipedia entry, Polymorphism in object-oriented programming.

Polymorphism with Java Abstract

Polymorphism come after study of class generalization relationship and object-oriented concepts such as Reuse, Subtype, Substitutable, Inheritance. The following depict “is-a”, inheritance relationship kind of polymorphism in Java. It uses of abstract class and abstract method at super class to let its subclasses to be implemented getArea() method.


Polymorphism with Java Interface

Sometime, we may want to use polymorphism which is not really “is-a” inheritance relationship at class/object analysis and design modelling stage. That is, there won’t be able to do superclass-subclass and make use of abstract class/method generalization as mentioned above. There may be somewhat constituting same behavior in given domain context. Then, we can use Java interface to still practice the polymorphism. This is somewhat better Java programming practice.

I just try to make CampusArea class to deliberately try to differentiate from Shape “is-a” generalization with Square and Circle, i.e. CampusArea should not be (is not meaningful of) subclass of Shape. And Java interface GeometricInterface is created and whichever (concrete) classes that implement it shall do the actual getArea() method implementation, and here by using abstract keyword to make it strengthen the implementation model, i.e.. concrete class must implement but you may relax this if you want in your model. This basic concept also manifested into Java AWT/Swing GUI interfaces, Java multiple interface inheritance(i.e., behavior/method level, not at property) concept and independently implementable interfaces on Java collection framework.

References:

  1. Anthony Lee W.H., Unit 5:Polymorphism and Object Reference, ICT201 Further programming and Object-oriented Concepts Using Java, SIM University
  2. Anthony Lee W.H., Unit 2:Object-oriented Approach, ICT203 Foundations in Modern System Design, SIM University
  • http://www.blogger.com/profile/15035698472954785554 Sigfreed77 Tech

    can you also make a GUI with JPanel and JFrame program with this same concept(the concept in this post)?