Pramod Ravikant
Pramod Ravikant

Reputation: 1037

Default class that is extended by all classes in java

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?

Upvotes: 6

Views: 9989

Answers (10)

user12774292
user12774292

Reputation: 1

import java.util.*; default class in java

Upvotes: -1

Saurabh Thakur
Saurabh Thakur

Reputation: 102

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

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49432

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");
} 

Upvotes: 10

Gopal Barge
Gopal Barge

Reputation: 61

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

Upvotes: 2

bNd
bNd

Reputation: 7640

Class Object is the root of the class hierarchy. Every class has Object as a superclass.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234875

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

Upvotes: 4

mrak
mrak

Reputation: 2906

"All Classes in the Java Platform are Descendants of Object": http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41240

Yes it is and it is extending Object class.

Object is root class of all java classes.

Upvotes: 2

Raphaël
Raphaël

Reputation: 3751

Yes, it is extending java.lang.Object.

Upvotes: 1

voidMainReturn
voidMainReturn

Reputation: 3517

yes all the classes by default extend Object class in java. Is that what you wanted?

Upvotes: 1

Related Questions