• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question from mock exam

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
list < ? super String> list = new ArrayList <String>> ();


can anybody help me to understand this syntax

2)
howcome a protected is less restrictive than default.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
list < ? super String> list = new ArrayList <String>> ();

The above syntax says that the list is declared of type list and can take only objects of type String or super class of String.
and it has been initialized as an ArrayList which will accept only Strings in the list.
What is the problem in it ???



2)
howcome a protected is less restrictive than default.

Protected is less restrictive than defualt cause the visibility of the object or method marked protected is visible outside the package to all the classes who extend that class . Where as default is limited to visibility just with in that package irrespective of whether the class is being extended by some other class out side package it won't b visible.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!

When you have List < ? super String> l;
There are two important points to remenber:

1) You can assign to l a list of String or Object higher than String
eg: List < ? super String> l = new ArrayList <String> ();

2) You can directly add to list String's object and noting higher.
e.g: You cannot do l.add(new Object);

Hope it's help.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic