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

Recommended Answers

All 5 Replies

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

Interfaces are also typically required when you are writing distributed systems.

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.

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.? :)

Interfaces are placeholders. They ensure the must be implemented subset of methods are implemented by all that uses the interface. Kind of way to ensure a given set of operations are always supported by all who implement the inerface.

----------------------

Programming (Assignment/Project) Help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.