Reputation: 153
Hi I am relatively new to programming and I am studying inhertance at the moment.
There is an exmaple in a book that has a class which does not extend any other class(So is it fair to say this is the superclass) also there is another class which extends this class. But what I really cant get is the class which does not extend any other class contains @Override. I thought @Override was only used in subclasses to override the superclass.
Any help would be really appreciated
Upvotes: 2
Views: 96
Reputation: 200296
The only class which does not extend any other class is Object
. All other classes derive from it either directly or indirectly.
Your example with a class "which doesn't extend any other" looks something like this:
public class MyStuff {
@Override public String toString() { return "MyStuff here"; }
}
In the above, extends Object
is implied if not written. Therefore you should read it as
public class MyStuff extends Object { ... }
There are just three public methods in Object
which it makes sense to override: toString
, equals
, and hashCode
, so I assume your example uses one (ore more) of these.
Upvotes: 2
Reputation: 3794
In java Object
class is the super class of all classes. So @Override
method must be belongs to Object class methods.
Upvotes: 2