Logout

Polymorphism

(And Non-overloaded Constructor Examples)

Polymorphism, generally speaking, is using something many different ways. (Poly means many, and morph means to change.) Both operators and methods can be used many differnent ways in Java.

So polymorphism is actually quite a simple concept. But it was a big deal, and something different when it came along, bundled with the hype of OOP. Do note that you don't necessarily have to have an objects-based approach to programming to have polymorphism, but the two things came along in programming circles in about the same time, right around when Java was developed.

Polymorphism can take two general forms: overloading, and overriding.

Overloading

Overloading allows either an operator or a method to act differently depending on the context in which it is used. The "context" for an operator will be the data types of the operands on either side of it, and the "context" of a method is how many and what type of parameters is sent to it.

The easiest operator example of polymorphism is the + operator, which works differently if between two ints or between two doubles, or even between two Strings. Beteeen two ints, for example, it implements arithmetic addition of whole numbers, yet between two Strings, it implements concatination, i.e. the joining together of, two Strings. So, same


        System.out.println(2 + 4); //Output: 6
        System.out.println("Hello" + "world"); //Output: Helloworld

And the easiest method example of polymorphism would be the overloading of constructors. If one constructor of a class takes in one String, another constructor takes in two Strings, and third takes in a String and an int, all three constructors functions differently, but the name of the constructor method is the same in all three cases; it's just the parameter list which changes.

   
public
LunchCard() { } public LunchCard(String userName, int cardBalance, boolean isStaff) { this.userName = userName; this.cardBalance = cardBalance; this.isStaff = isStaff; } public LunchCard(String userName, int cardBalance) { this.userName = userName; this.cardBalance = cardBalance; }

Though overloaded constructors are an example of polymorphism, they would probably be considered a trivial example with regards to dossier Mastery Aspects. More likely if you use polymorphism for a Mastery Aspect, it would be a sophisticated algorithm you came up with specifically for your dossier, which can execute more than one way depending on the parameters you send. Though not so original, implementing a couple of different versions of your searches or sorts, would count. So, without putting the whole code down, something along the lines of:


    public int[] selectionSort(int []){
        .....
        .....
        ... < ...
    }
    
    public String[] selectionSort(String []){
        .....
        .....
        ...compareTo(...) < 0 ...
    }
    

A Note on Terminology

I've used the following bolded terms before, but only occasionally: We would say that above, what makes the methods overloaded is that the signatures of the methods are different due different parameter lists. The other term used for the first line of a method is method header. So put another way, the method header is the line, and the signature relates to the specifics of the line. But anyway, with regards to polymorphism, the methods above are an example of that becaue even though the method names are the same, the signatures are different.

 

Overriding

We override a method by taking an already existing method from a class we have inherited and re-doing it to work differently, even though it takes in the same kind, type, and order of parameters. We can also do the same with an interface which we have implemented; interfaces are not that big a deal, but are not part of the IB course.

The classic example of polymorphism of a Java method is overriding the toString() method, which is one of the Object class' methods, and so a method which all other Java classes inherit, since Object is the "mother of all classes".

       
       LunchCard lc = new LunchCard("John", 1000, true);
       System.out.println(lc.toString()); //Output before overriding: John1000true
           
       //Now if we ourselves override toString to be as follows:
            
       public String toString(){
           return "This is silly, but this is what toString will print out each time.";
       }
            
       //So then, this is how the lines would work:
            
       LunchCard lc = new LunchCard("John", 1000, true);
       System.out.println(lc.toString()); //Output after overriding: This is silly, but this is what toString will print out each time.