ExamVeda
Login
Home
This question belongs to Java Program Inheritence
Inheritence
?

What will be the output?
class A{
      int i = 10;
      public void printValue(){
            System.out.print("Value-A");
      }
}

class B extends A{
      int i = 12;
      public void printValue(){
            System.out.print("Value-B");
      }
}

public class Test{
      public static void main(String args[]){
            A a = new B();
            a.printValue();
            System.out.print(a.i);
      }
}

Answer & Solution
Correct Answer: Option B

If you create object of subclass with reference of super class like ( A a = new B();) then subclass method and super class variable will be executed.

Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Raghavendra Sureban
Raghavendra Sureban 10 years ago
The explanation given for the code is right but I think the options for this questions are wrong(there is no correct answer in it).