+ 1

What is the meaning of this line " Class[] theInterfaces = o.getClass().getInterfaces();" in the following code

Note : - this code is from hackerrank and in this program if you give a single input as a number then it returns the addition of all possible divisors . foe ex. :-> divisors of 6 are 1,2,3,6 and the addition of all divisor is 12. here is the code import java.util.*; interface AdvancedArithmetic{ int divisor_sum(int n); } class MyCalculator implements AdvancedArithmetic { public int divisor_sum(int n) { int sum = 0; int sqrt = (int) Math.sqrt(n); for (int i = 1; i <= sqrt; i++) { if (n % i == 0) { // if "i" is a divisor sum += i + n/i; // add both divisors } } /* If sqrt is a divisor, we should only count it once */ if (sqrt * sqrt == n) { sum -= sqrt; } return sum; } } class Solution{ public static void main(String []args){ MyCalculator my_calculator = new MyCalculator(); System.out.print("I implemented: "); ImplementedInterfaceNames(my_calculator); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.print(my_calculator.divisor_sum(n) + "\n"); sc.close(); } /* * ImplementedInterfaceNames method takes an object and prints the name of the interfaces it implemented */ static void ImplementedInterfaceNames(Object o){ Class[] theInterfaces = o.getClass().getInterfaces(); for (int i = 0; i < theInterfaces.length; i++){ String interfaceName = theInterfaces[i].getName(); System.out.println(interfaceName); } } }

6th Jan 2021, 2:19 PM
Yash Chaudhari
Yash Chaudhari - avatar
1 RĂ©ponse
+ 1
getClass() returns the class that the object belong to. getInterfaces() returns an interface array that are implemented in the class. So o.getClass().getInterfaces() means get the array of interfaces which are implemented in the class that is o's data type.
6th Jan 2021, 3:06 PM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar