Java class design question.
What are the benefits\drawbacks about the design below?
Here is a simple example of the possible class design.
What are the benefits of doing it this way? vs. just dropping the interface class all together.
=================================
Public interface Car
{
public String getColor();
public void setColor();
}
==================================
public class CarImplement implements Car
{
private String color;
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
}
===================================
thanks
Sailor_Jerry
Junior Poster in Training
88 posts since Aug 2005
Reputation Points: 13
Solved Threads: 2
In this case i dont think there is any specific benefit of using interface.
Interfaces are useful when the child classes have same methods which have different implementation.
More than that interfaces are useful when we require multiple inheritance
yni420
Junior Poster in Training
92 posts since Oct 2004
Reputation Points: 7
Solved Threads: 0
uh, not just distributed systems and in distributed systems too it depends on the design of the system :)
The Interface defines your public interface. Handy when you have multiple implementations. Typically you don't want any class using your classes (instead of creating instances of them) to have to care about what actual implementation type they're dealing with so you write those classes against the Interface instead.
You can then send them something else completely as long as it implements that Interface and it'll work.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Thanks for you help.
jwenting. I am not 100% clear on your post. But I think i am the right track.
My thinking for creating interface for my getters and setters is to promote maintainability.
I want to do this for all my classes that have the getter and setter logic.
I don’t totally understand the benefits.
jwenting. i think you were saying doing this will give me the flexibility to change the class the implements the interface, without breaking any other code.? :)
Sailor_Jerry
Junior Poster in Training
88 posts since Aug 2005
Reputation Points: 13
Solved Threads: 2