oop - Can method overloading be considered a way of describing polymorphism in Java? -
so, studying head first java , there read -
an overloaded method different method happens have same method name, has nothing inheritance , polymorphism.
however, have famous book named java - complete reference herbert schildt , in book, read:
method overloading 1 of ways java supports polymorphism. method overloading supports polymorphism because 1 way java implements “one interface, multiple methods” paradigm.
which 1 of them correct?
can method overloading considered way of describing polymorphism in java?
no these 2 different concepts.
i think incorrectly understood author means.
i stressed in bold important part :
method overloading 1 of ways java supports polymorphism. method overloading supports polymorphism because 1 way java implements “one interface, multiple methods” paradigm.
it means overloading , polymorphism not incompatible.
for example, may have interface defines overloaded method :
public interface doable{ void dox(string s); void dox(integer s) }
suppose mydoable
implementation of , mychilddoable
subclass of mydoable
.
when write :
mydoable mydoable = new mychilddoable(); mydoable.dox("a")
the compiler bound invoked method void dox(string s);
(overloading) @ runtime, method of instance (mychilddoable
) used (polymorshism).
Comments
Post a Comment