Nami Nami - 3 months ago 9
Java Question

Method headers allowed by the compiler (simple Java exercise)

I am practicing and the simple exercise is, given a FeatureFilm class defined to have the following methods:

public void update(Actor a, String title)
public void update(Actor a, Actor b, String title)
public void update(String topic, String title)


which of the following additional method headers would be allowed by the compiler?

public boolean update(String category, String theater)
public boolean update(String title, Actor a)
public void update(Actor b, Actor a, String title)
public void update(Actor a, Actor b)


So I did the code and the compiler doesn't allow this methods:
public boolean update(String category, String theater)
and
public void update(Actor b, Actor a, String title)
, but I don't entirely understand why. Someone could explain this to me please?
I hope to be making good use of this site. I'm a OPP beginner.
Sorry about my (poor) English,
Thanks.

Answer

The signature of a method in Java is made up of the number, type, and order of arguments, but not the return type.

Not allowed:

public boolean update(String category, String theater)
public void update(Actor b, Actor a, String title)

These methods have the same signature as ones which already appear, and the return type does not determine the signature. The trick here is that even though the return types are different, the signatures are still the same.

Allowed:

public boolean update(String title, Actor a)
public void update(Actor a, Actor b)

These methods have different signatures from the ones which already appear, because the type and order of parameters are different.