+ 1

why it is printing "class A method"

class A{ static void method(){ System.out.println("Class A method"); } } class B extends A{ static void method(){ System.out.println("Class B method"); } } public class Mcq29{ public static void main(String args[]){ A a = new B(); a.method(); } }

11th Apr 2017, 5:21 AM
Somnath Ghosh
Somnath Ghosh - avatar
3 odpowiedzi
+ 1
Because your method is overlapping each other, if you call class A in your main class you should get A only, take a look at the example https://code.sololearn.com/cHYuF7yaKSVm/?ref=app
11th Apr 2017, 5:35 AM
John Watson
John Watson - avatar
+ 1
john,,i have used static method
11th Apr 2017, 5:43 AM
Somnath Ghosh
Somnath Ghosh - avatar
+ 1
So static methods are restricted to superclass.
12th Apr 2017, 9:02 PM
John Watson
John Watson - avatar