aspose file tools*
The moose likes Beginning Java and the fly likes Abstract/Interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract/Interface question" Watch "Abstract/Interface question" New topic
Author

Abstract/Interface question

Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
Posts: 27
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

Joined: May 26, 2003
Posts: 30762
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
Posts: 27
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

Joined: May 26, 2003
Posts: 30762
    
156

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

Joined: Mar 16, 2005
Posts: 27
Been searching the forums and think i understand now. Thanks
[ March 16, 2005: Message edited by: Clay Adkerson ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract/Interface question