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

06-14-2011, 06:46 PM

elliotHenry

Quote:

Originally Posted by Toll

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

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

Quote:

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?

06-14-2011, 06:50 PM

Toll

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

06-14-2011, 07:00 PM

elliotHenry

Quote:

Originally Posted by Toll

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:

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.

06-14-2011, 07:58 PM

JosAH

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:

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

06-14-2011, 08:33 PM

elliotHenry

Quote:

Originally Posted by JosAH

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:

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

06-14-2011, 08:37 PM

Toll

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

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

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:(party):

06-14-2011, 08:52 PM

Toll

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?

06-14-2011, 08:55 PM

elliotHenry

Quote:

Originally Posted by Toll

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).:D:

06-14-2011, 09:00 PM

Toll

... Interfaces. And inheritance. And probably some other technical terms.

06-14-2011, 09:18 PM

elliotHenry

Quote:

Originally Posted by Toll

... Interfaces. And inheritance. And probably some other technical terms.