As we know every class in Java inherits from java.lang.Object class. But why ?
There could be lots of reasons to support this argument:--
1. As per java recommendations, every class must have some common features like every class must be able to represent itself in human readable form which is a string, (toString()) , every object must be able to check it's identity ( equals() ), clone() etc. So Java designers have put common methods like toString(), clone(), equals() etc in Object class, and every other class inherits from Object class.
2. wait(), notify(), notifyAll() play a very crucial role in thread synchronizing in multithreading. Every object must have these methods. Hence Java designers have put these methods in Object class and every class inherits from Object class.
3. As we know java collections holds only Objects, and hash code and equality is very important for collections to work. Hence equals() and hashcode() methods are part of Object class.
4. For garbage collection, finalize() method is invoked, which is part of Object class.
5. Every object must know about it's class at runtime. getClass() method gives the details about the class at run time.
Also It is said that Java supports only single inheritance, then what would happen in case if class A is extending from class B?
If you create a new class in Java and you specify that it "extends" from some other class say class B, then your class inherits from class B and so on and class B in turn inherits from class Object.
If you create a new class in java and don't an "extends", then your class implicitly extends from Object class.You don't have to write any code for this. Java compiler and JVM run time will automatically treat your class a direct sub class of Object class.
There could be lots of reasons to support this argument:--
1. As per java recommendations, every class must have some common features like every class must be able to represent itself in human readable form which is a string, (toString()) , every object must be able to check it's identity ( equals() ), clone() etc. So Java designers have put common methods like toString(), clone(), equals() etc in Object class, and every other class inherits from Object class.
2. wait(), notify(), notifyAll() play a very crucial role in thread synchronizing in multithreading. Every object must have these methods. Hence Java designers have put these methods in Object class and every class inherits from Object class.
3. As we know java collections holds only Objects, and hash code and equality is very important for collections to work. Hence equals() and hashcode() methods are part of Object class.
4. For garbage collection, finalize() method is invoked, which is part of Object class.
5. Every object must know about it's class at runtime. getClass() method gives the details about the class at run time.
Also It is said that Java supports only single inheritance, then what would happen in case if class A is extending from class B?
If you create a new class in Java and you specify that it "extends" from some other class say class B, then your class inherits from class B and so on and class B in turn inherits from class Object.
If you create a new class in java and don't an "extends", then your class implicitly extends from Object class.You don't have to write any code for this. Java compiler and JVM run time will automatically treat your class a direct sub class of Object class.
No comments:
Post a Comment