Is there any Default Class that is extended by all the classes by default in Java?
Example: If I have a simple class like:
Class A
{
String a;
}
Is this class extending a class by default?
java.lang.Object class is superclass of all classes.
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
You can test it :
A a = new A();
if(a instanceof Object){
System.out.println("Object is superclass of all classes");
}
In Java, everything (apart from the plain old data types; int, boolean, double etc.) is implicitly derived from java.lang.Object.
In particular, the class contains useful functions such as lock() and notify() which are used in thread synchronisation.
For a full list, see http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html
Yes it is and it is extending Object class.
Object is root class of all java classes.
"All Classes in the Java Platform are Descendants of Object": http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
java.lang.Object is the super clas of all the classes. all the Java provided classes or the class which you create by your self all are the sub class of Object class by default
yes all the classes by default extend Object class in java. Is that what you wanted?
Yes, it is extending java.lang.Object.
Class Object is the root of the class hierarchy. Every class has Object as a superclass.
yes "Object" class is root class for all other classes. Here is an example to prove it to find the package and class using the Object reference variable.As you can see I have not included Object class explicitly to the project but still I can assign the reference variable to the class "Object" and use it as the class "FindingClass" is inheriting the Object class,Object class reference variable can now access the "FindingClass" object.It is possible only when the current class "FindingClass" is inheriting Object class.
package Chapter9.Packages;
class FindingClass{
}
public class FindClass {
public static void main(String[] args) {
Object obj;
FindingClass fcls = new FindingClass();
obj=fcls;
System.out.println(obj.getClass());
}
}
Output:
class Chapter9.Packages.FindingClass
来源:https://stackoverflow.com/questions/17187218/default-class-that-is-extended-by-all-classes-in-java