Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Low coupling??

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

An enthuware question, on coupling and cohesion, asks what the following signifies:

Using reference variable for an interface type instead of a class implementing the interface

Answer: Low Coupling





Can you help me understand this statement and why this answer..

I know that low coupling means one class is not very dependent on another... that low coupling provides for very little/no modification in other classes. I do know that an interface-type reference variable can be used to refer to an implementor-class's object. But I don't understand much of that statement...
 
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In answering a certification question, the other options are also important. Can you post also the other options? That is, kindly post the entire question and all the given choice of answers.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Low coupling indicates the favourable situation where the all that is known of a class is through its interface declaration It is independent of the implementation details. So even if the implementation ic changed, but the API for the interface declaration remains same, then the classes referring to it won't be affected.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Its a DnD question that goes like this..

Identify the situation described on the left by the terms specified on the bottom:

1)Using reference variable of an interface type instead of a class implementing the interface // what?

2)A class that provides only one specific functionality //ok. high cohesion

3)A class that directly accesses the members of another class
//ok. high coupling

4)Changing the implementation of a class doesn't affect the classes that use it.
//ok. low coupling
Options:
Low coupling
High coupling
Low cohesion
High cohesion




Yes, Dean I understand that.. but dont quite understand this first statement...
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using reference variable of an interface type instead of a class implementing the interface

Look @ the following code:


Here interfaceDemo is an example of a reference variable of an interface type instead of a class implementing the interface.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Using reference variable for an interface type instead of a class implementing the interface

Answer: Low Coupling



To me, the above quote makes sense in that if you have a reference variable that is typed as any class, then that reference variable, parameter, or return type can only be that exact class or any superclass of that exact class.

If you instead use an interface type as a variable, parameter, or a return type, there is more flexibility. Then, any class that implements the interface can be referred to and your code is more flexible because there is a higher number of classes that the object reference variable can refer to because the object reference variable only has to implement an interface. (Remember that you can extend from one class, but you can implement many interfaces).

Does what I wrote make sense?
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kaydell,



1)When a reference type is a class, say
MyClass k;
then k can be assigned, either a new MyClass() or
new MyClassSubClass(),...

2)but whereas if it is an interface-type say
MyInterfaceType j; then j can be assigned any new MyImplementorClass1(), new MyImplemntorClass2() or these implementing classes' subclasses, even when MyImplementorClass1 is not in the same hierachy as MyImplementorClass2

This is what you mean, to say right?

Ya, so my the interface-type reference can take now a lot of different-class's objects, and that would make the code flexible...

But flexibility and low-coupling are related??
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you understood what I wrote.

But flexibility and low-coupling are related??



That's a good question. I made the point that using variables typed as interfaces are more general, but that wasn't exactly the question that you asked.

In answer to your question about "coupling", with the objects declared as classes, you know more about the other class. You know more about the type. You know more about its class hierarchy. With objects declared as interfaces, you know less about the other class. All you know is that it must implement that interface. This is less coupled because you know less about the object being referenced in the object reference variable when the object reference variable is declared as an interface than you know when the object reference variable is declared as a class.

As a general rule, whenever you have an abstract class, consider using an interface instead. Sometimes an abstract class is better and sometimes an interface is better. I believe that this is a judgement call whether to use object reference variables declared as a class or as an interface. Declaring an object reference variable is called "Programming to an interface" and is generally better than "programming to a class".

According to the book "Effective Java": Favor interfaces over classes when declaring variables, parameters, and return types.
[ January 13, 2008: Message edited by: Kaydell Leavitt ]
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ya, the statement

Using reference variable of an interface type instead of a class implementing the interface ==> signifies low coupling



makes proper sense to me now... Thanks. I'd thought before, why we should create an object and then instead of a class-reference-type use an interface-reference type for it might restrict the number of methods that can be accessed in this second case.. But now I guess, that's what is desirable, restricting the access.

Thanks
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
    Bookmark Topic Watch Topic
  • New Topic