in Java can one instance method hide another method? -
overriding case:
class a{ public void m() {} } class b extends { @override public void m(){} }
hiding case :
class { public static void m(){} } class b extends a{ public static void m(){} }
is 1 instance method hiding another?
interface { void m(); } interface j { void m(); } class implements i,j { void m(){} }
can instance method hide instance method?
class { public static void m(){} } class b { public static void m(){} }
these classes don't share common ancestor, 1 can't "hide" other's method. in java, same-name instance methods in subclasses override superclass's instance method, there no hiding mechanism there in other languages (such pascal if remember correctly). true if @override
annotation isn't made, annotation makes code more readable , helps against typos.
if changed class b
extend class a
, however, it's static method m()
in fact hide same name method in a
.
this the oracle docs have this:
the distinction between hiding static method , overriding instance method has important implications:
- the version of overridden instance method gets invoked 1 in subclass.
- the version of hidden static method gets invoked depends on whether invoked superclass or subclass.
in case of interfaces:
interface { void m(); } interface j { void m(); } class implements i,j { void m(){} }
interfaces don't provide instance methods themselves, contract implementing class has honor implementing methods declared in interface. in case, both interfaces require implementing classes (like class a
) implement method public void m()
. it's promising both wife , daughter take them out ice cream on saturday - promise made daughter doesn't invalidate, hide or negate same promise made wife. ;)
so in short: instance methods cannot hidden in java.
as side note: per java naming conventions, classes , interfaces should start upper case letters.
Comments
Post a Comment