• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract/Interface question

 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34208
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clay,
Welcome to JavaRanch!

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.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34208
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Been searching the forums and think i understand now. Thanks
[ March 16, 2005: Message edited by: Clay Adkerson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic