Write a method that counts the number of methods implemented in a class. This me
ID: 3625808 • Letter: W
Question
Write a method that counts the number of methods implemented in a class. This method will not count the methods that are inherited from a superclass and not overridden in the current class.The method will be called methodCount and must:
- Be declared public;
- Be an instance method;
- Take one parameter of type Object, instance of the class to be considered ; and
- Return an int containing the number of methods implemented in the class.
Notes:
- methodCount returns 0 for a null object given as argument.
- You can use the methods of java.lang.reflect.*.
Explanation / Answer
public int methodCount(Object obj) { int count = 0; if(obj != null) { Class c = obj.getClass(); Method[] meths = c.getDeclaredMethods(); count = meths.length; } return count; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.