• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

S extends CharSequence

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. given the following:



The question is

Given that CharSequence is an interface implemented by String and StringBuilder which of the following can be inserted at insert here to compile and run without error



One of the answers is:

s.toString();

According the API the signature of toString() is

public String toString()

which means that object returned by such a call is a String object which extends CharSequence and thus meets the requirements of <S extends CharSequence>

However this is the wrong answer..

Why? S has to extend CharSequence however if I declare a type S that extends CharSequence... my input can be a StringBuilder, but my return type can be a String ... they both fit the requirement.. or it is that S has to be the same type S for all s thus the class of the input and the output in this case must be an S??? Does that follow polymorphism rules? can s be a subtype of S as long as IS-A S is true?



I dunno.. loosing hope of passing this test.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is that S has to be the same type S for all s thus the class of the input and the output in this case must be an S???


Yes this is correct. If your input is StringBuilder, then your method will work like
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic