aspose file tools*
The moose likes Beginning Java and the fly likes Object Oriented Enunciate (low coupling) 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 "Object Oriented Enunciate (low coupling)" Watch "Object Oriented Enunciate (low coupling)" New topic
Author

Object Oriented Enunciate (low coupling)

Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
"The reference variable is declared for an interface type, not a class. The interface provides a small number of methods."

I don't understand what this means ...

could you represent it in code, please ?

thanks in advance
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Juan,
Consider "Comparable comp = new Date();"

The Comparable interface has less methods than the Date class. Defining comp as a Comparable rather than a Date narrows down the number of methods available which clarifies the code. This helps decrease the coupling because unnecessary dependencies aren't floating around.


[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
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
Comparable comp = new Date();

why do you want to do that ?
what does it make ?

Comparable comp;

is the same thing
Mark Williams
Ranch Hand

Joined: Aug 01, 2008
Posts: 66
So in effect, what you end up with is a Date object where only the methods defined by the Comparable interface are accessible?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Originally posted by Juan Manuel Alberto de los Santos:
Comparable comp;

is the same thing

Yes. Except that it doesn't create an object. I was trying to illustrate both parts of the declaration.

Originally posted by Mark Williams:
So in effect, what you end up with is a Date object where only the methods defined by the Comparable interface are accessible?

Exactly!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Hi,

excuse my ignorance here, but I am slightly confused. In relation to the above code:



I was of the opinion, that the JVM during late binding, looks for how the object was created, not the reference, so should not the methods of the Date class, be the available methods, as despite being a Comparable reference type, the object comp was created as a Date. Thus should it not have the methods of the Date class?

To re-iterate Mark's question:

So in effect, what you end up with is a Date object where only the methods defined by the Comparable interface are accessible


I believe it to be that, you end up with a Date type which "IS A" Comparable, of type Date and therefore the methods are of Date class (which by inheritance also include Comparable methods.

Of course, I'm not an expert, but this is my understanding.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
If you want to use that object as a Date you would have to cast it.
And we all know how hazardous class-casting is.

Declaring it as Comparable means that only the methods in the Comparable type are accessible.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Thanks Campbell, although I believe its a bit dificult for us newcomers to get our heads round. I supose its polymorphism in practice. :roll:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Practice is the word; lots of practice is required, and then you will get used to it.

And remember only non-private instance methods can be overridden, so only non-private instance methods can exhibit polymorphism
 
jQuery in Action, 2nd edition
 
subject: Object Oriented Enunciate (low coupling)