1

I remember on my previous web development job that they have something like this:

sampleClassBean.java:

public class sampleClassBean
{
    public String doSomeStrings(String a, String b){}

    public Int doSomeInt(Integer i, integer j){}

    public Boolean doSomeBoolean(Boolean result){}
}

and then there's sampleClassBeanImpl

public class sampleClassBeanImpl
{
  public String doSomeStrings(String a, String b)
  {
     //do some process
     return "";
  }

public Integer doSomeInt(Integer i, Integer j)
  {
     //do some process
     return 0;
  }

public Boolean doSomeBoolean(Boolean result)
  {
     //do some process
     return false;
  }
}

For what I understand is that, there's 2 class, 1st class that declare methods, now 2nd class's methods will depend on what is declared on the 1st class. If the 2nd class create a method that is not declared in 1st class there will be an error. Hope you understand what i'm saying.

What I need to know is what exactly that? What do you call that process? How to do that? What are the benefits of doing that? Is it a good programming practice?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
HjReyes
  • 41
  • 6
  • Are you talking about inheritance or an interface? – SPlatten Dec 07 '17 at 10:51
  • The answers to this question may help you https://stackoverflow.com/questions/12899372/spring-why-do-we-autowire-the-interface-and-not-the-implemented-class – Zain Dec 07 '17 at 11:17

1 Answers1

3

Yes it is good practice, you are talking about interfaces:

Java includes a concept called interfaces. A Java interface is a bit like a class, except a Java interface can only contain method signatures and fields. An Java interface cannot contain an implementation of the methods, only the signature (name, parameters and exceptions) of the method.

The interface:

public interface SampleClassBean {
    public String doSomeStrings(String a, String b);
    public int doSomeInt(int i, int j);
    public Boolean doSomeBoolean(Boolean result);
}

And the implementation:

public class SampleClassBeanImpl implements SampleClassBean {
    @Override
    public String doSomeStrings(String a, String b) {
        return null;
    }

    @Override
    public int doSomeInt(int i, int j) {
        return 0;
    }

    @Override
    public Boolean doSomeBoolean(Boolean result) {
        return null;
    }
}

Interfaces are really useful because unlike other languages Java doesn't support multiple inheritance but you can implement all the interfaces you wish!

Have a read of this it'll help you understand interfaces and when to implement them.

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29