Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - declaring from Interface type instead of Class

Tags:

java

interface

In my quest to correctly grasp Interface best practices, I have noticed declarations such as:

List<String> myList = new ArrayList<String>(); 

instead of

ArrayList<String> myList = new ArrayList<String>(); 

-To my understanding the reason is because it allows flexibility in case one day you do not want to implement an ArrayList but maybe another type of list.

With this logic, I set up an example:

public class InterfaceTest {      public static void main(String[] args) {          PetInterface p = new Cat();         p.talk();      }  }  interface PetInterface {                      public void talk();  }  class Dog implements PetInterface {      @Override     public void talk() {         System.out.println("Bark!");     }  }  class Cat implements PetInterface {      @Override     public void talk() {         System.out.println("Meow!");     }      public void batheSelf() {         System.out.println("Cat bathing");     }  } 

My question is, I cannot access the batheSelf() method because it only exists for Cat. That leads me to believe that I should only declare from an Interface if I am only going to use methods declared in the Interface (and not extra methods from the subclass), otherwise I should declare from the Class directly (in this case Cat). Am I correct in this assumption?

like image 901
jexx2345 Avatar asked Aug 01 '10 20:08

jexx2345


People also ask

Why do we use interface instead of class in Java?

Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.

Can you use an interface as a type in Java?

You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

Can an interface be a declared type?

You can declare variables to be of an interface type, you can declare arguments of methods to accept interface types, and you can even specify that the return type of a method is an interface type.

Can we use interface as a variable?

You shouldn't put any variables inside Interfaces. Because interfaces define contracts which can be implemented in various ways. The value of a variable is implementation. We certainly can when we know all the classes implementing the interface have some constant variables(Field names for instance).


2 Answers

When there is a choice between referring to an object by their interface or a class, the former should be preferred, but only if an appropriate type exists.

Consider StringimplementsCharSequence as an example. You should not just blindly use CharSequence in preferrence to String for all cases, because that would deny you simple operations like trim(), toUpperCase(), etc.

However, a method that takes a String only to care about its sequence of char values should use CharSequence instead, because that is the appropriate type in this case. This is in fact the case with replace(CharSequence target, CharSequence replacement) in the String class.

Another example is java.util.regex.Pattern and its Matcher matcher(CharSequence) method. This lets a Matcher be created from Pattern for not just String, but also for all other CharSequence there are out there.

A great example in the library of where an interface should've been used, but unfortunately wasn't, can also be found in Matcher: its appendReplacement and appendTail methods accept only StringBuffer. This class has largely been replaced by its faster cousin StringBuilder since 1.5.

A StringBuilder is not a StringBuffer, so we can not use the former with the append… methods in Matcher. However, both of them implementsAppendable (also introduced in 1.5). Ideally Matcher's append… method should accept any Appendable, and we would then be able to use StringBuilder, as well as all other Appendable available!

So we can see how when an appropriate type exists referring to objects by their interfaces can be a powerful abstraction, but only if those types exist. If the type does not exist, then you may consider defining one of your own if it makes sense. In this Cat example, you may define interface SelfBathable, for example. Then instead of referring to a Cat, you can accept any SelfBathable object (e.g. a Parakeet)

If it does not make sense to create a new type, then by all means you can refer to it by its class.

See also

  • Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces

    If appropriate interface types exist, then parameters, return values, and fields should all be declared using interface types. If you get into the habit of using interface types, your program will be much more flexible. It is entirely appropriate to refer to an object by a class if no appropriate interface exists.

Related links

  • Bug ID: 5066679 - java.util.regex.Matcher should make more use of Appendable
like image 173
polygenelubricants Avatar answered Oct 06 '22 02:10

polygenelubricants


Yes, you are correct. You should declare as the most general type providing the methods you use.

This is the concept of polymorphism.

like image 30
Borealid Avatar answered Oct 06 '22 04:10

Borealid