Examveda
Examveda

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);
      }
}

A. Value-B 11

B. Value-B 10

C. Value-A 10

D. Value-A 11

E. None of these

Answer: Option B

Solution(By Examveda Team)

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.


This Question Belongs to Java Program >> Overriding And Overloading

Join The Discussion

Comments ( 1 )

  1. HARSHITA RASTOGI
    HARSHITA RASTOGI :
    6 years ago

    import java.io.*;

    class Base{
    int i = 8;
    static void call(){
    System.out.println("Base");
    }
    }
    class Derived extends Base{
    int i = 10;
    static void call(){
    System.out.println("Derived");
    }
    }
    class Derived2 extends Derived{
    int i = 12;
    static void call(){
    System.out.println("Derived2");
    }
    static void call2(){
    System.out.println("call2");
    }
    }
    class Test{
    public static void main (String[] args) {
    Base b = new Derived2();
    System.out.println(b.i);
    b.call();
    }
    }

    gives output as:
    8
    Base

Related Questions on Overriding and Overloading