• 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

OCP Practice Exams Sef-Assessment Test 2 Q11

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Please, this question is from the OCP Java SE 6 Programmer Practice Exams textbook, Assessment Test 2 question 11. Below is the code for the question:



class A{}
class B extends A {}
class C extends B {}




I don't quite understand the syntax of the generic declaration at the class level(line 1) and at the method level(line 2). Anyone care to explain them to me and also point me to a basic beginner's tutorial on Java Generics?

Thanks
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with line 1:

This means that when you instantiate a Carpet class, you say what type it should be. You are limited in those types to classes that extend B (or are B). Which means new Carpet and new Carpet<C> are valid. However new Carpet is not valid because A doesn't extend B.

Line 2 is more confusing.

It's saying that the return type must be a subclass (or the same class) as V. We don't know what V is at compile time. It could be B or C or some future subclass to be created. The rest of line 2 is irrelevant to the question in the book, but it further restricts what can be passed into the method.

This isn't a beginners topic, but here is the
Oracle trail on generics and bounds.

In this book, the assessment test is meant to give you a feel for whether you are ready to take the other/harder tests in the book. Maybe you want to pick up Kathy and Bert's other book that actually explains Java and prepares you for the exam, before you go on to the rest of this book?
 
reply
    Bookmark Topic Watch Topic
  • New Topic