Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Abstract/Interface question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41263
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41263
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Been searching the forums and think i understand now. Thanks
[ March 16, 2005: Message edited by: Clay Adkerson ]
 
I was her plaything! And so was this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic