Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Toll

Thread: Specimen exam quesiton

  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Specimen exam quesiton

    Hi folks

    Ok, so I've been cramming for my exam tomorrow and my brain has now decided it doesn't want to participate anymore, so I'm looking for help from the experts.

    I've been working through a specimine exam (this'll be the forth this week) and this quesiton came up:

    {
    {
    Thing aThing;
    ...
    Which important feature of Java allows aThing to be
    declared as being of type Thing, even though there is no class with this name.
    Names have been changed to protect the innocent

    Anyways, can someone point me to where I can answer this? I know I should know this already but as I said, my brain won't participate anymore

    Thanks

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Well, technically interfaces aren't classes, but I'm not sure that's the answer they're after.

  3. #3
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    Well, technically interfaces aren't classes, but I'm not sure that's the answer they're after.
    Hi Toll

    Perhaps the class header will help?

    public class SomeThing
    As you can see, it isn't implementing any interface.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Huh? What does the class header of SomeThing have to do with an object of type Thing, where Thing isn't a class?

  5. #5
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    Huh? What does the class header of SomeThing have to do with an object of type Thing, where Thing isn't a class?
    Beats me, that's why I posted this here. Here's the full code
    Java Code:
    public class SomeThing
    
    {
    Thing aThing; 
    ...
    Which important feature of Java  allows aThing to be
    declared as being of type Thing, even though there is no class with this name.
    With the question

    Thing aThing;
    ...
    Which important feature of Java allows aThing to be
    declared as being of type Thing, even though there is no class with this name.
    See what I mean?

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    My answer still holds... Technically, an interface isn't a class. They might be after something else though, for all I know.

  7. #7
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    My answer still holds... Technically, an interface isn't a class. They might be after something else though, for all I know.
    Cheers Toll

    Glad it's not as obvious as I first thought. I would have implemented the interface in the class header as follows:
    Java Code:
    public class SomeThing implements Thing
    Then I wouldn't need an instance variable for Thing at all, just the methods that Thing implements.

    Oh well, ho hum. If this one comes up tomorrow I'll only drop 2 points.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    If Thing isn't a class it must be an interface; Java knows three kind of types: interfaces, classes (which are types with implementation) and primitive types (int, double etc.) Compare your example with an analogous:

    Java Code:
    List myList= new ArrayList();
    Nowadays the List and ArrayList would've been a List<T> and ArrayList<T> but for the sake of the example: a List is a type and an ArrayList is a type with implementation. In Java the implementation has to implement the interface to make sense.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If Thing isn't a class it must be an interface; Java knows three kind of types: interfaces, classes (which are types with implementation) and primitive types (int, double etc.) Compare your example with an analogous:

    Java Code:
    List myList= new ArrayList();
    Nowadays the List and ArrayList would've been a List<T> and ArrayList<T> but for the sake of the example: a List is a type and an ArrayList is a type with implementation. In Java the implementation has to implement the interface to make sense.

    kind regards,

    Jos
    Hi Jos

    Ok, thanks. Both you and Toll seem to be in agreement. I don't disagree with either of you, and you may well be right (well, of course your right). Seems odd though that my learning center has broken their own convention during an exam paper. As per my previous post, this isn't how an interface would be used.

    Thanks for all the posts folks. I thought it was me being a bit brain dead but when your explicitly told to implement something one way, then they throw a spanner in the works by doing it different then you can only expect confusion.

    Cheers

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Actually, I think you misunderstand how they're used. Say you have the following code:

    interface Thing{}
    class FirstThing implements Thing{}
    class SecondThing implements Thing{}
    class ThirdThing implements Thing{}

    You can then do the following:

    Thing aThing;
    aThing=new FirstThing();
    aThing=new SecondThing();
    aThing=new ThirdThing();
    Last edited by Toll; 06-14-2011 at 09:38 PM. Reason: typo

  11. #11
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    Actually, I think you misunderstand how they're used. Say you have the following code:

    interface Thing{}
    class FirstThing implements Thing{}
    class SecondThing implements Thing{}
    class ThirdThing implements Thing{}

    You can then do the following:

    Thing aThing;
    aThing=new FirstThing();
    aThing=new SecondThing();
    aThing=new ThirdThing();
    Hello again Toll

    Thanks again for your post. Not to labour it too much, I agree that what you've stated above would be just fine, but the class header in the question doesn't implement the Thing interface

    Java Code:
    public class Something
    note, the quesiton doesn't drill any further down, but want's me to explain what the important feature of Java is that allows me to do this. So if the answer is "allows me to implement interfaces where disparet classes can responde to the same messages in individual ways" or something to that affect then hey presto, two points in the bag

  12. #12
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    The class Something doesn't have to implement Thing in order to use objects of that type.

    Consider yourself of class Person. You then have a variable of type Key, that can be set to be a HouseKey, a CarKey, a BicycleKey and any other kind of key. You don't have to be a Key in order to use a Key, right?

  13. #13
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    The class Something doesn't have to implement Thing in order to use objects of that type.

    Consider yourself of class Person. You then have a variable of type Key, that can be set to be a HouseKey, a CarKey, a BicycleKey and any other kind of key. You don't have to be a Key in order to use a Key, right?
    Yes, agreed. So what is the important feature of Java that lets me do this? Sorry, brain is now in full shutdown mode. I think I need a sleep/beer/more beer (delete as appropriate).

  14. #14
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    ... Interfaces. And inheritance. And probably some other technical terms.
    elliotHenry likes this.

  15. #15
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    ... Interfaces. And inheritance. And probably some other technical terms.


    Yes, that'll do it. Cheers again.

Similar Threads

  1. Sun Exam Vouchers
    By dtkmn in forum Java Certification
    Replies: 2
    Last Post: 04-28-2010, 01:31 PM
  2. Java 6 Exam
    By varsh in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2010, 07:14 PM
  3. Sun exam vouchers
    By aji1729 in forum Java Certification
    Replies: 2
    Last Post: 11-05-2009, 12:43 PM
  4. Preparing for Exam
    By AWPtic in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 03:34 PM
  5. SUN SCJP 310-065 Exam
    By seedfox in forum Java Certification
    Replies: 0
    Last Post: 03-27-2009, 11:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •