+ 1

In the below code , why the method inside super class is not called. I was expecting the o/p as three, 3 but the crt o/p is 0,3

class Super { Super() { printThree(); } void printThree() { System.out.println("three"); } } class Test extends Super { int three = (int)Math.PI; // That is, 3 void printThree() { System.out.println(three); } public static void main(String[] args) { Test t = new Test(); t.printThree(); } } This program produces the output: 0 3

28th Jan 2019, 3:10 PM
Lakshmi Krishnamoorthy
1 Réponse
+ 2
You're overriding void printThree() { System.out.println("three"); } with void printThree() { System.out.println(three); } When the constructor of the class "Super" gets called, printThree() of the new Test object is executed, that is void printThree() { System.out.println(three); } void printThree() { System.out.println("three"); } doesn't "exists" in Test class
31st Jan 2019, 3:35 AM
unChabon
unChabon - avatar