About Question com.enthuware.ets.scjp.v6.2.95 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.95 :

Post by ETS User »

Consider the following code:
import java.util.*;
class Book{ }
class TextBook extends Book{ }

class BookList extends ArrayList<Book>
{
public int count = 0;
public boolean add(Object o)
{
if(o instanceof Book ) return super.add((Book) o);
else return count++ == -1;
}
}

//in valid context
BookList list = new BookList();
list.add(new Book());
list.add(new TextBook());
list.add("hello");

System.out.println(list.count);

What will it print?

A: It will not compile.
Observer that BookList extends a typed ArrayList, which is typed to Book. Therefore, the add(<E> ) method in ArrayList has been typed to add(Book). Hence, BookList cannot override add(Book) method with add(Object) method. The overridden method can use a subclass for the parameters but not a superclass.
------

I wonder why in this case we consider the 'add(Object o)' method as an overridden one? Why it is not might be overloaded add method? If this assumption true, It will not compile only due to the fact that o is "hello" in 'if(o instanceof Book )' - String has no relation to hierarchy of Book. Where I am wrong?

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by admin »

Think of it this way: All generic information is removed at runtime. So actually, add(<E> ) is same as add(Object) to the JVM. Therefore, you cannot overload add(<E>) with add(Object). It would be considered overriding.
If you like our products and services, please help us by posting your review here.

J.Andrés

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by J.Andrés »

A: It will not compile.
Observer that BookList extends a typed ArrayList, which is typed to Book. Therefore, the add(<E> ) method in ArrayList has been typed to add(Book). Hence, BookList cannot override add(Book) method with add(Object) method. The overridden method can use a subclass for the parameters but not a superclass.

8-) If the method use a subclass for the parameters will not result in a legal overload?

class Book
{}

class ElQuijote extends Book
{}

class MyBookList extends ArrayList<Book>
{

@Override
public boolean add(ElQuijote o)
//Legal overload not a override
{
return super.add(o);
}

}


Really "public boolean add(Object o)" is valid override in runtime , not in compile time. With the type erasure the method will be again "public boolean add(Object o)" , curious at least, no?

I am right?

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by admin »

You are right. The statement is misleading and should be fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests