I thought I understood interfaces....then along came a collection
Hi all
I'm having a bit of trouble understanding how the following works:
HashMap vehiclesOnRunway = new HashMap();
public Vehicles[] getVehiclesOnRunway(){
Collection vehicles = vehiclesOnRunway.Values();
return (Vehicles[])vehicles.toArray();
}It works ok, but I want to understand how the interface gets its implementation. I would understand if vehicles was a concrete class, or if HashMap.Values() returned a concrete class.
My question is: if vehicles is a Collection (interface) and HashMap.Values() returns a Collection (interface) then where does the implementation come from for Collection.Values()?
Does anybody know the answer to this? Or could you recommend a good book or online source?
Kind regards
Tim
It works ok, but I want to understand how the
interface gets its implementation. I would understand
if vehicles was a concrete class, or if
HashMap.Values() returned a concrete class.And what if Vehicles was an abstract class? Would you understand it then? Because its the same idea.
What if Vehicles was a non-final concrete class? Would you understand it then if the returned reference was to an object whose type was a subclass? (e.g., return type is Shape, the returned reference is toa Square)? Because its the same idea.
You can check the runtime type of the returned value by printing vehicles.class; Then, look at the source code or documentation for the relevant class, and you will see that if implements Collection.
Here's a short example that you can run and that will hopefully help:
interface MyInterface {
void myMethod();
class MyClass implements MyInterface {
public void myMethod() {
System.out.println("MyClass.myMethod called on "+this);
class MyOtherClass implements MyInterface {
public void myMethod() {
System.out.println("MyOtherClass.myMethod called on "+this);
public class InterfaceTest {
private static MyInterface getMyInterfaceObject() {
int i = (int)(100 * Math.random());
System.out.println(i);
if (i % 2 == 0) {
return new MyClass();
else {
return new MyOtherClass();
public static void main(String[] args) {
MyInterface obj = getMyInterfaceObject();
System.out.println(obj);
obj.myMethod();
}This demonstrates that the code in the main method can call methods on both MyClass and MyOtherClass, because they implement MyInterface. The code in the main method does not know what the actual runtime type of the reference returned from the getMyInterfaceObject method will be - just that it will be a type that implements MyInterface. The getMyInterfaceObject method decides at runtime what type of object to return - either a MyClass or a MyOtherClass; In this case the decision is made according to whether or not a random number is odd or even, but it could be made based on any other criteria, such as looking the type up in a configuration file. Equally, it does not need to be made at runtime, but even if it were made at compile-time (which it usually is), the developer knows that they are free to change the type later and it won't break client code.
Similar Messages
-
My paid Student Adobe Cloud had Acrobat XI installed, then along came Acrobat DC. It uninstalled XI, and will not install DC?
I hit the same problem except I had a Reader X installed not a pro version. For me the solution was to really make sure that nothing else was running. I followed the steps from http://support.microsoft.com/kb/2438651#reso2 the link is goofy so here is the pertinent text:
Programs that are currently running on your computer may be interfering with the installation processDisable programs that might be interfering with the installation process. To do this, follow these steps:
Click Start
, type msconfig in the Search box, and then click msconfig.exe.
If you are prompted for an administrator password or confirmation, type the password, or provide confirmation.
On the General tab, click Selective startup, and then click to clear the Load startup items check box.
Click Services, click Hide all Microsoft services, and then click Disable all.
Click OK, and then click Restart.
Try running your software program installation.
When you are finished troubleshooting the problem, restart the computer in Normal Startup mode. To do this, follow these steps:
Click Start
, type msconfig in the Search box, and then clickmsconfig.exe.
If you are prompted for an administrator password or confirmation, type the password, or provide confirmation.
On the General tab, click Normal startup, click OK, and then click Restart. -
Starting in 2015 I get the following error when I try to import my photos. "The following files were not imported because they could not be read" -DSC8416.NEF I've been importing the same type of photos with no errors then 2015 came along and now I can no longer import.
The error message is misleading, the problem is that you need WRITE privileges added to the destination directory tree.
Assuming you are using the "date" structure that LR defaults to, find the directory that contains the 2014 folder (the one that works) and add WRITE rights to the parent of "2014".
While you are there, if you want, add "2015" folder to beside "2014" and add READ and WRITE rights to that new directory. -
Using Suri asking any localised question in the UK, it responds with 'cannot help' can only use US locations etc. and in US English. Have I missed something or set something up incorrectly? I also thought it would interface with your Facebook but it just comes back with a 'I can't help you with Facebook' message
Yeah, Siri - a headline feature on the iPhone 4s page - is beta at the mo.
But then anyone who's used Macs for a few years will know that most new software is pretty much still in beta when it's released anyway. Only Apple could get away with it and retain customers -
I pre ordered a album and got songs then it came out so itunes sent me a confirmation and it said to go to available downloads and gave link so I did and it did nothing for me and when I went to the album I wanted it said it was purchased so how do I get my music and don't even see available downloads in the iTunes Store
Check this link, http://www.apple.com/support/itunes/ and go to the contact page.
Sorry, here you go https://ssl.apple.com/emea/support/itunes/contact.html
Message was edited by: ChrisJ4203 -
I was in a conversation with a colleague and a text (identical) came to both of our phones, when he researched it, it said it was blocked and I thought I might see where it came from through my phone. Is it possible to get a spam like that to our phones at the same time.If the text came to my number marked from him, it could only have been from him?. Is there a possibility that someone else sent it to both of us at the same time?
Yes, it could be spam. I get spam text from time to time. Yes, you both could have gotten the same spam at the same time. Spammers re sending out thousands and thousands of messages.
-
Have just updated my iphone 4 to ios 5, is the notification centre supposed to appear on your main screen as an icon if so how do i get it to show, and i thought you could now use the camera from the locked sreen i can't seem to this at the moment can anyone help? thank you
Double click home button for camera to show up (and iPod). Notifications seem to show up on lock screen that way, too.
-
My Earpiece doesn't work fine. I tried restoring it on iTunes, work's for 2 days then problem came again. Help me on this thanks.
Go buy yourself a iPhone 5. Sync your iPhone 3GS to iTunes on you computer so you have a back up ready to put on the iPhone 5. If you want you can update your iPhone 3GS with iTunes on your computer, but this is not necessary.
-
I thought I understood automator but I must be missing some basic understa
Thought I understood automator but I must be missing some basic understanding. I want select several audio (mp3 stereo) files and get automator to convert them to mono aiff files. I know how to do this in soundtrack pro, and I looked under actions, library, music; but could not find a way to get them to be mono in automator. Suggestions?
thanks
J Jordan
The KiteSites.com SeriesI'm sorry, I downloaded and installed 'drop a few my way' and read the instructions, but where is the 'drop a few my way' icon? Mysterious new files appeard in the Library/iTunes/Scripts/ folder, but where is the icon supposed to be?
-
Hi. I have a Macbook Pro Model 5,5 with one 800 firewire port. I need to create two firewire 800 ports but only have an SD card slot (this is for video interface with 2 cameras). Is this possible or do I have to have an express card slot in order to do this?
Hi Jessica,
No, the SD slot will not work, you need an ExpressCard/34 slot.
I do not know if it works with cameras, but with two or more HDs, if the first one has two FW ports, you can "daisy chain" one or more additional HDs from it. -
I had my iPod 5th generation in the wash for about 3-5 minutes, I took it out of my jacket and made the mistake of trying to turn it on. The first time it came on, then never came back on, its been in rice for 6 days, should I try anything else? Please help!
Try:
- iOS: Not responding or does not turn on
- Also try DFU mode after try recovery mode
How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
- If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
- Try another cable
- Try on another computer
- If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
Apple Retail Store - Genius Bar
Apple will exchange your iPod for a refurbished one for $99 for a 16 GB 5G and $149 for the other 5Gs. They do not fix yours.
Apple - iPod Repair price -
My adobe Photoshop opens, loads full interface, then crashes without any error message etc.
Help?If there is no error from the OS - then it did not crash, just exited.
-
In app purchase, error "contact Itunes store support to complete your purchase". But I have already bought many then suddenly came this error msg. Why?
To Contact iTunes Support and request assistance Click Here
-
I've opened up iTunes and went to buy a song, but then it came up with a box saying "this is the first time you are purchasing an item on this computer" even though my computer is authorised. Then when i put in my password it takes me to the security questions and i dont know what the answers are. What do i do?
A computer can be authorized to play content purchased using a particular Apple ID (the iTunes Store account). However, being authoized to play content is not directly related to being able to buy content using that Apple ID.
For example, a parent may purchase content for a child and authorize the child's computer to play that content, but that does not mean the child is able to purchase content using that Apple ID.
If this is your Apple ID, you should know the answers to the security questions. If you do not, you'll need to contact Apple iTunes Store customer support
http://www.apple.com/support/itunes/contact/ -
once I sync my photos from my PC to my iphone where do I find them on the iphone? I went to camera icon, then to camera roll, but all I see are the photos I took on the iphone.
There are two ways to view ITHMB files. The first is to convert them to a more standard format, such as JPEG. You can try doing this with the help of the free utility, ithmbconv. The second way is to use the program iThmb Converter. It will let you view files without converting.
Maybe you are looking for
-
Updating SRM contracts from R/3 to SRM....
Hi all, I am working in SRM, my requirement is to update the SRM contracts from R/3 to SRM and if any changes happen in R/3 contracts (previously updated contracts to SRM) then it should replicate those changes in SRM contracts also. I found the prog
-
How to sync notes with iOS 4.2.1?
Hi, since iTunes was updated I miss the possibility to sync notes between my Mac and my iPod Touch 2G with iOS 4.2.1. The function disappeared from iTunes. How can I sync my notes without iCloud (because iOS 4.2.1 does not have iCloud)? Steffen
-
When typing text in forms, the vertical bar gets multiplied all over the place.
Often when I'm typing text into a form on various sites, be it Facebook or this very site, the vertical bar that follows the input text gets randomized all over the line and appears multiple times. The glitch disappears about half a second after I st
-
Where I can get the javac.exe source code?
because I wanna to rewrite it in my own way. Thanks
-
Email header printing on separate page
Not sure what I did, but now when I print an email, the header (to/From:) prints on the first page and then the main email prints from page 2 on. I can not see where this can be changed. They used to print together as they do on my other Macs. Jus