What is the output for the below code?
public class A{
int add(int i, int j){
return i+j;
}
}
public class B extends A{
public static void main(String argv[]){
short s = 9;
System.out.println(add(s,6));
}
}
public class A{
int add(int i, int j){
return i+j;
}
}
public class B extends A{
public static void main(String argv[]){
short s = 9;
System.out.println(add(s,6));
}
}
A. Compile fail due to error on line no 2
B. Compile fail due to error on line no 9
C. Compile fail due to error on line no 8
D. 15
E. None of these
Answer: Option B
Solution(By Examveda Team)
Cannot make a static reference to the non-static method add(int, int) from the type A. The short s is autoboxed correctly, but the add() method cannot be invoked from a static method because add() method is not static.
Join The Discussion
Comments ( 2 )
What is a constructor in Java?
A. A special method to create instances of classes
B. A method used for mathematical calculations
C. A method to perform string manipulations
D. An exception handling mechanism
In Java, which method is automatically called when an object is created?
A. start()
B. main()
C. init()
D. constructor()
What is method overloading in Java?
A. Defining multiple methods with the same name in the same class
B. Calling methods from another class
C. Using methods to load data from a file
D. Running methods in parallel threads
Answers should be B
above java program will give two error.
1. At line-1 or line-6(we can't define two public class in a java file)
2.line-9 (non static method is accessed from static method)