This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, i need help understanding interfaces. I understand that they are abstract classes that contain all abstract methods, which are methods that have no code, just the method itself for another class to complete. This is from something explaining the basics of Interfaces:
What i dont understand is why do you need to Implement Clock if the other classes, Watch and Cuckoo, contain public String GetTime(int hour);. They contain that method so why do you need the Interface Clock.
Clock is a contract that says you will implement the getTime method. [It's conventional to start methods with a lowercase letter.] This is advantageous because other methods can refer to clock. For example,
This student class is more flexible because it isn't tied to a specific type of clock.
Thanks for the quick reply but i don't really understand still. Do you just have to reference the interface itself and it wil choose which class, Cuckoo or Watch to use the getTime method? I know it is a lot more complicated then that but just trying to get the basic idea. Im taking Java in highschool and my teacher isn't very communicative and doesn't really know how to teach.
author & internet detective
Clay, The class creating the Student needs to know the concrete class.
So Registrar knows that we are dealing with a watch. But Student only needs to know about the Clock interface. This makes Student a more flexible class.
Suppose you change the name of the Watch class to WristWatch. Think about which classes need to change. In my example, only Watch (obviously) and Registrar need to change. If Student was passed a Watch instead of a Clock, it would need to change too. In a bigger, the impact of this is even more significant.
Joined: Mar 16, 2005
Been searching the forums and think i understand now. Thanks [ March 16, 2005: Message edited by: Clay Adkerson ]