0

We can use base class functions by extending from subclass.Generally we use equals() method which is defined in Object class.I read in the book that every class will extend Object class and so that we are able to use functions like equals() in our user defined class with subclass reference.

One doubt i am having is with out extending Object class (Even any other class that extends Object class) we are able to use equals method.

can any one explain how it happens?

3 Answers 3

1

Every class in Java extends Object by default(unexplicitly) no matter what you do.

Sign up to request clarification or add additional context in comments.

Comments

0

If you do:

Class Foo { }

the compiler will treat it as if it were

Class Foo extends Object { }

Even when classes can only have at most 1 super class, if you do

Class Foo extends Bar { }
Class Bar extends Biz{ }

at certain moment up in the hierarchy one of those classes will have the class Object as it's parent, so even when only the topmost class would directly extend from Object, all of the other classes down the hierarchy would indirectly extend Object too, hence giving you access to Object class defined methods such as: clone, equals, finalize, etc...

Regards.

Comments

0

It's an implicit rule, JVM may implement the mechanism.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.