Danish Butt Danish Butt - 3 months ago 11
Java Question

How do I read Java's API documentation?

I have some confusion on what class name to use from the documentation.

If I want to use the method

getLength(Object name)
from the
Array
class the compiler accepts
java.lang.reflect.Array.getLength(nameOfArray)
but not
java.lang.Object.getLength(nameOfArray)
. Though the picture of the API documentation linked below seems to imply to me that both classes are linked to the Array classes method.

Java API documentation example

Answer

The reason you see them linked is that Object is a Superclass of Array.

In Java, Object is the superclass of all classes.

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class.

More over here: https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

Each such child class (Array class in this case) inherits some methods from the superclass (Object class in this case).

If the child class wishes to change any of these variables or methods that it inherits from the superclass, then it will override those. In addition, the child class itself may have its own variables and methods which have got nothing to do with the superclass. That is the case in the example you've shown i.e. Array is a data-structure that has a length, but its parent Object does not have a length.

Note: Please read about Inheritance in Java.
In addition, also read about access specifiers (because private members are not inherited), keywords (eg. final variables can't change value, final methods cannot be overriden, final classes can't be subclassed). Good luck!

Comments