[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

JAVA Programming :: Basic Concepts - Discussion

Home > JAVA Programming > Basic Concepts > MCQs Questions Discussion

47 / 64

What is the Output of the below code.

public class Base {
 public void method(int i) {
  System.out.println("Base class Value is" + i);
 }
}
public class Sub extends Base {
 public void method(int j) {
  System.out.println("Sub class Value is" + j);
 }

public static void main(String args[]) {
 Base b1 = new Base();
 Base b2 = new Sub();
 b1.method(5);
 b2.method(6);
}
}

ABase class Value is 5 Sub class Value is 5

BBase class Value is 5 Sub class Value is 6

CBase class Value is 6 Sub class Value is 6

DNone

Answer: Option (Login/Signup)

Show Explanation

On the other hand, line no. 12 i.e, Base b2=new Sub(); we have created an object of Sub class but it's reference is of Base class. In such a case when b2.method(6); is invoked, Sub class method gets invoked, and we get the output as Sub class value is 6.  

Asked In ::

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here