πŸ“Š JAVA
Q. 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));
}
}
  • (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) Compile fail due to error on line no 8 D. 15
πŸ’¬ Discuss
βœ… Correct Answer: (B) Compile fail due to error on line no 9

Explanation: 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.

Explanation by: Praveen Singh
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
67
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Praveen Singh
Publisher
πŸ“ˆ
92%
Success Rate