0

I've got a task to do polymorphism but I am not entirely sure I understand the concept as per testimony of my teacher.

According to web definitions and examples, this by all means is polymorphism, but they say it is not. Can I please get confirmation?

OversizedParcel.java

public class OversizedParcel implements ParcelType {

    public void resolve(PrivateUser user) {
        //do theese
        //and those
    }

    public void resolve(LegalUser user) {
        //do different thing
        //and a completely different thing
    }
}

IllegalParcel.java

public class IllegalParcel implements ParcelType {

    public void resolve(PrivateUser user) {
        //do this
        //do that
    }


    public void resolve(LegalUser user) {
        //do a thing
        //do a different thing
    }
}

(hypothetical class)

public class Main{

    private User user; //loaded user
    private List<ParcelType> parcels; //assume this contains the loaded parcels already

    public static void main(String[] args){
        for(ParcelType type : parcels) type.resolve(user);
    }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jack Avante
  • 1,405
  • 1
  • 15
  • 32
  • 1
    **but they say it is not** have you asked your teacher to provide justification for that decision? – Ivan Apr 02 '19 at 15:28
  • [Wiki](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)), first sentence. Your code satisfies this. – MC Emperor Apr 02 '19 at 15:29
  • Yes it is polymorphism. – vijay Apr 02 '19 at 15:31
  • 2
    You do have an issue here, that this will probably not compile, as your interface (which you didn't specify) doesn't have a method `resolve(User)`, and as Java determines which method to call by the argument's **static type**, your use of overloading won't work. – RealSkeptic Apr 02 '19 at 15:33
  • By the way, no need to append `Type` to the class name here, just use `Parcel`. – Basil Bourque Apr 02 '19 at 15:56
  • As RealSkeptic commented, you have a problem with `User` versus `LegalUser` and `PrivateUser`. And you need to edit the Question to show your interface `ParcelType`. Voting to close as the Question is incomplete and cannot be answered precisely. – Basil Bourque Apr 02 '19 at 16:00
  • Follow the Java naming conventions. Doing so will make your work easier. Class names begin with an uppercase letter. I fixed it for you here. – Basil Bourque Apr 02 '19 at 16:08
  • Possible duplicate of [Polymorphism and interfaces - clarification?](https://stackoverflow.com/questions/25163683/polymorphism-and-interfaces-clarification) – armitus Apr 02 '19 at 17:58

5 Answers5

1

Polymorphism can be defined as -

it is the ability of an object to take on many forms

. The most common example of polymorphism could be-

when a parent class reference is used to refer to a child class object.

So as per your question, in most simplistic way polymorphism can be defined as

ParcelType oversizedparcel = new oversizedParcel();
ParcelType illegalparcel = new illegalParcel();

Here ParcelType can be a oversizedParcel or illegalparcel

So if your understanding is as per my answer, then indeed it is an example of polymorphism.

Smaranjit
  • 659
  • 7
  • 17
1

I'd like to offer a dissenting opinion from what appears to be majority here. Keep in mind that "Polymorphism" is a fairly flexible term, and that what is written here does not necessitate 100% universal truth. This is simply something to aid the balance of thought.

No, what you have written is not polymorphism. This is due to the fact that they instantiate different unrelated objects that simply implement the same interface.

Traditionally, Polymorphism occurs when you have a child object that overrides a parent object's implementation of a method. Hence, there is "multiple forms" of a method that exist at the same time at different levels of the object's vertical hierarchy.

However, interfaces are merely an agreed-upon contract of inputs and outputs that standardize interactions. They don't by themselves hold an instance of the code (we shall exclude default interface methods for the sake of this conversation). Because of this, there is no "Re-definition" of the interface within an object. the same object tree does not instantiate multiple versions of the interface (unless it is through the traditional view of polymorphism).

Even if a method required two arguments of interface ParcelType, it does not necessarily mean polymorphism, it simply means that the method is asking for two 'boxes' of a particular shape and size. These boxes are empty until they are passed into the method as two distinctly different objects that are referenced separately (And not the same method object being overridden by a child object, for example)

Different objects can take advantage of the interface contract, and in a way you can say that it is "Horizontal Polymorphism", but I think this is taking away from the intention of what polymorphism means in the context of Java.

armitus
  • 712
  • 5
  • 20
0

According to the W3School definition, it is indeed polymorphism. Anyway, if your teachers said it is not, they may have been expecting you to do something else.

Polymorphism is, to go further than just an example, an entire concept meaning that you can do entirely different things by using "the same things", or more exactly "things named the same".

Have a look on the Wikipedia definition, which is more complete than any language-specific one, to have a wider view on it.

0

Polymorphism is having the same thing in different forms. So, Yes this is polymorphism.

LeoN
  • 1,590
  • 1
  • 11
  • 19
0

I assume that resolve is defined in ParcelType interface. Then the type.resolve calls in for(ParcelType type : parcels) type.resolve(user) are dispatched polymorphically on ParcelType

David Soroko
  • 8,521
  • 2
  • 39
  • 51