Why extends object
An example of such a class is provided in the Oracle tutorial Object Ordering. Reimeus already pointed out that what you're asking for in your edit isn't possible. I'd just like to expand a little on why. In fact that's what came to my mind when I first saw this post. But this actually gives a compiler error:. To help me explain why, I'd like to quote an Oracle Blogs post by Victor Rudometov about this error:.
This rule applies to all cases including type variables and bounds in methods and constructors. The reasons for this restriction are explored in a closely related post: Why can't I use a type argument in a type parameter with multiple bounds?
Specifically, extending a generic interface twice with different parameters. I can't come up with a non-contrived example, but:.
Now holder. Chris also points to Sun bug , which was a bug contesting this language restriction. It was closed as Won't Fix with the following comment:.
If a type variable could be followed by type variables or by possibly parameterized interfaces, there would likely be more mutually recursive type variables, which are very difficult to handle. Things are already complicated when a bound is simply a parameterized type, e. Consequently, bounds are not going to change now. So those are the reasons behind the restriction. Addressing generic methods specifically which your question concerns , I'd like to further point out that type inference would theoretically cause such bounds to be pointless anyway.
Assuming the caller isn't explicitly specifying T and U , this can be reduced to the following:. This is because T doesn't have any bounds, so no matter what type of arguments get passed in, T can always resolve to Object at the very least, and so then can U. So why is this example legal? If it seems strange that an object can belong to more than one type, remember that this happens in real life too. Every cat is also a mammal, and every mammal is also an animal.
But not every animal is a mammal, and not every mammal is a cat. The Deck and Hand classes we have defined so far could be used for any card game; we have not yet implemented any of the rules specific to Crazy Eights.
Here is the beginning of the Player definition:. A Player has two private attributes: a name and a hand. In this example, we have to use this to distinguish between the instance variable and the parameter with the same name.
The primary method that Player provides is play , which decides which card to discard during each turn:. The first parameter is a reference to the Eights object that encapsulates the state of the game coming up in the next section. The second parameter, prev , is the card on top of the discard pile. Since we have not written them yet, this is an example of top-down design.
If there are no cards that match, it returns null. In that case, we have to draw cards until we get a match, which is what drawForMatch does:. The loop uses the Eights object to draw a card. If it matches, drawForMatch returns the card.
This method is a straightforward translation of the rules of the game:. In Section In this way of developing programs, we identify high-level goals, like shuffling a deck, and break them into smaller problems, like choosing a random element or swapping two elements.
In this section, we present bottom-up design , which goes the other way around: first we identify simple pieces we need and then we assemble them into more-complex algorithms. Now we can start implementing the pieces. Here is the beginning of the class definition for Eights , which encapsulates the state of the game:.
In this version, there are always two players. One of the exercises at the end of the chapter asks you to modify this code to handle more players. The Eights class also includes a draw pile, a discard pile, and a Scanner , which we will use to prompt the user after each turn. The constructor for Eights initializes the instance variables and deals the cards, similar to Section The first line saves the top card from discardPile.
The next line transfers the rest of the cards to drawPile. Then we put the saved card back into discardPile and shuffle drawPile. We can use reshuffle as part of the draw method:. The nextPlayer method takes the current player as a parameter and returns the player who should go next:. The last method from our bottom-up design is displayState. It displays the hand of each player, the contents of the discard pile, and the number of cards in the draw pile.
Finally, it waits for the user to press the Enter key:. It reads the top card off the discard pile and passes it to player. No, thank you, we will need to add casts everywhere. The List interface extends the Collection and Iterable interfaces in hierarchical order. It implements the List interface so we can use all the methods of List interface here.
The ArrayList maintains the insertion order internally. Active 3 hr before. Not doing so leads to a compilation error when trying to use a class method from package A that expects an argument type of the class in in package A, and passing in an object generated from the equivalent class in package B. As java is an object oriented language, it supports inheritance which inherits the properties of the another class, for example all java objects inherits from java. Object class.
From the above example it is understood that it is the explanation of inheritance. Note that all classes, whether they state so or not, will be inherit from java. Object by default. You can see it here. I'm for adding it in - not everyone "implicitly" knows that every Java class implicitly extends Object. By writing it explicitly they don't have to guess.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Does 'extends Object' have a purpose or is it redundant? Ask Question. Asked 11 years, 9 months ago. Active 2 years, 7 months ago. Viewed 27k times. Does this syntax has any purpose or is 'plain dumb'?
Improve this question. Kylar 8, 7 7 gold badges 41 41 silver badges 73 73 bronze badges. Andrei Ciobanu Andrei Ciobanu There's also about a dozen other answers to this - search on SO and look. Add a comment.
0コメント