java - Is Method overloading allowed across the classes? Please explain why and how? -
my code:
public class main { public static void main(string[] args) { system.out.println("hello world!"); b b = new b(); b.p(); } } class a{ void p(){ system.out.println("a"); } } class b extends a{ void p(int a){ system.out.println("b :"+a); } }
is method overloading allowed across classes? because working in java. according concepts highly doubt in c++ , c# gives error java compiler invokes correct version of function not expected.
please explain why , how ?
yes overloading works here because class b
inherits overloaded method class a
. specified in java language specification:
if 2 methods of class (whether both declared in same class, or both inherited class, or 1 declared , 1 inherited) have same name signatures not override-equivalent, method name said overloaded.
Comments
Post a Comment