• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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.
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic