Chandhan Sai
Chandhan Sai

Reputation: 103

How create my own custom Interface using Java with Example

I have created an interface for my business class. I have my business logic which displays the name. I want to display the name by using an interface. I heard that in order to hide the business logic we use interface but how do I do it? This is what I have tried but I don't know how to use it.

public interface Business_Logic_Interface {
    public String getName();
}

public class Business_Logic implements Business_Logic_Interface {
    private String name = "I am business data";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class MainApp implements Business_Logic_Interface {
    public static void main(String[] args) {}

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return null;
    }
}

I want to display the name by using interface.

Upvotes: 3

Views: 4566

Answers (3)

nits.kk
nits.kk

Reputation: 5336

Abstraction hides the implementation but exposes the contract.

Having said that now lets see how. Suppose you are given a responsibility of implementing a business logic. Now you would probably take some time in building the logic , unit test the same, refactor the logic again unit test the same. Meanwhile another team (or teammate) needs to consume the logic written by you but they do now want to wait for completion of your logic implementation.

In such a case you would expose the object type and its behaviour. Object type and behaviour becomes the contract, the interface. Interface consists of its name and abstract methods. Interfaces are nothing but the predefined contract. It is something which is fixed but its implementation may change but as the contract is fixed hence the change always adhers with the contract and those who depend on the contract are un affected by the change. Name of interface becomes object type (concrete type is decided later). So now having the interface exposed now comes the consumption part.

The users of your code will consume the contract you defined. They without knowing the implementing classs can not do new so they consume your implementation by dependency injection (simply via. constructor or setter methods). Even if the consumer code knows the implementing class today still it should avoid being dependent upon the implementation as it may change tomorrow. It mush depend upon the contract which is fixed.

Upvotes: 1

vincrichaud
vincrichaud

Reputation: 2208

An interface is designed to be implemented by multiple classes. It's a way to add a level of abstraction. It allows you to call different object through the interface, so you don't have to bother what object you are manipulating since you know they implement your interface.

In your case, to show you the utility I will add a second class implementing the interface :

public class Business_Logic_2 implements Business_Logic_Interface{
    private String name = "I am a second class";

    public String getName() {
        return name + ", hello you !";
    }
}

Then in your main method, I create two different objet, one Business_Logic and one Business_logic_2. Then I'll put them in a List of Business_Logic_Interface. By doing like that I can put different objects in the same list, since I'm expecting object implementing your interface. Later I can call the method getName() on the object without knowing if they are of type Business_logic or Business_logic_2. All I have to know is that they are implementing the interface.

public class MainApp {
    public static void main(String[] args) {
        Business_logic obj1 = new Business_logic();
        Business_logic_2 obj2 = new Business_logic_2();
        List<Business_Logic_Interface> list = new ArrayList<Business_Logic_Interface>();
        list.add(obj1);
        list.add(obj2);

        for(Business_Logic_Interface bli : list) {
            System.out.println(bli.getName());
        }
    }
}

Upvotes: 1

Eugen
Eugen

Reputation: 917

You are hiding business logic inside class that access to this logit will be across interface. MainApp must not extend Business_Logic_Interface it must contain variable with type Business_Logic_Interface

public class MainApp {
    public static void main(String[] args) {
        Business_Logic_Interface val = new Business_Logic();
        // acces to business logic
        val.getName();
    }
}

Upvotes: 2

Related Questions