What is a parameterized reference in Java?

What does the following Java statement mean "A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface."?

  • Ref: This statement is from SCJP book written Kathy Sierra. The interface is public interface Interface_Animals { public abstract void interfaceAnimals_Method(); } //////// class Animal implements Interface_Animals{ Animal animalanimal = new Animal(); public static void animal(){ } public void interfaceAnimals_Method(){ } } class Pets extends Animal{ public static void pets(){ } } class Dog extends Pets{ } //////// public class TestInterface{ public static void main(){ Interface_Animals interface_Animals = new Interface_Animals(); interface_Animals.animalanimal; interface_Animals.pets(); } } 1) Why can't I create a reference variable declared as Interface type? Interface_Animals interface_Animals = new Interface_Animals(); 2) As per the statement why can't I do the following? interface_Animals.animalanimal, because the statement says, a reference variable declared as a interface type can reference any object of any class that implements the interface. Here the declared reference variable is interface_Animals and the object we refer is of class Animal that implements the interface. 3) Why can't I do interface_Animals.pets()?

  • Answer:

    An interface is a way to describe the behavior of a class. Any concrete class that implements (this is a keyword in java) that interface must provide an implementation of each of the methods defined in the interface (unless it's an abstract class). See http://docs.oracle.com/javase/tutorial/java/concepts/interface.html. In general, the Java tutorials at the previous link are a great starting point for these types of questions. Answering your specific questions: 1. You can declare the variable as an interface, which you did on the left side of the equals sign. You however cannot create an instance of that interface because an interface is not a concrete implementation. You would need to do something like: Interface_Animals interface_Animals = new Animal() 2. You're trying to reference a variable that is declared in one of the concrete implementations of the interface. Because you declared the variable interface_Animals to be of type Interface_Animals, you can only call methods (or access constants) explicitly declared in the interface 3. Again, pets() is not a method declared in the interface and thus you don't have access to it via a variable declared to be of the interface type. Also, try to follow standard Java naming convention, here is a good reference: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5-naming

Tom Adamson at Quora Visit the source

Was this solution helpful to you?

Other answers

This is a sample of polymorphism, and also the major reason for even having an interface. As sample: It allows you to "plug-in" a different implementation into your code using minimal changes. Say (e.g.) you're using an LinkedList, then later you decide you rather want to try a ArrayList (just to see if it's more performant in this case). So you have a variable defined as a http://docs.oracle.com/javase/7/docs/api/java/util/List.html type. This interface defines how all the methods should look from the outside - not how they're implemented. So in your code you can use all the goodies like add, get, remove, etc. Now when you first write your code you instantiate the contents of that variable with a new LinkedList(). Then later you change just that one line to use new ArrayList() instead. And voila, no further changes needed, since you know that both ArrayList and LinkedList implement all the methods defined in the List interface. If however you had your variable set as a LinkedList type, you might have use the addLast method. Now changing to an ArrayList means you'd need to search through all your code to find all those method calls to LindekList-specific methods and change them to the ArrayList specific ones. See how this can become a lot more complicated? Note, this is only an example. You could even have the option of choosing between Array/Linked at runtime if you use this. A simple if statement can instantiate either one depending on some "setting". so you can even get the user to choose what they want to use.

Irné Barnard

You can't create an instance of an interface directly.  You must create an instance of an object that implements the interface.  The purpose of interfaces is to simplify the multiple inheritance problem from other object-oriented programming languages like C++.  Think of an interface as a personality.  An object can have multiple personalities based on the interfaces it implements. An interface defines a set of programming contracts and behaviors we expect from a component but leaves out the details.  In other words, an interface describes what we want an object to do and leaves the how to the implementor. It's a form of abstraction.  We as the user of the object only care that is does the job it defines.  We aren't particularly concerned as to how it does that job.  Kind of like when you press the accelerator in a car.  All accelerators make a care speed up or slow down.  That's the interface.  The engine is the implementation.  You have many different types and styles of engine design and behavior but you don't care what's under the hood.  You only care the car speeds up or slows down. To answer question #1, you have create an instance of class that implements the Interface_Animals interface.  That is an Animals, Pets or Dog class.  Any of of these can be referenced as Interface_Animals. On #2, the animalanimal member variable has default scope.  It is not visible to classes outside of the Animal class.  You'd have to scope the variable as public (public Animal animalanimal) to make it visible outside of the class.  FYI, this is a bad practice. On #3, you can't call the pets() method on a reference type of Interface_Animals because the Interface_Animals interface does not declare the pets() method.  That is part of the Animal class.  Only references of the Animal type can call that method. This can be confusing because you can see all of the code and can't figure out why Interface_Animals and Animal can't share.  They are different types.  An Animal is a Interface_Animals but Interface_Animals is not an Animal. In development, especially in Java, much of what we do revolves around a simple concept: Program to interfaces, not implementations.  We focus on the abstract of what an object does, not how.  We defer that to "someone else".  Virtually all modern Java frameworks and components are developed around this concept. Feel free to ask if you want some other examples to help clarify.  This is a rather advanced concept that takes time to absorb.  Examples are best to learn from.

Matt Pickering

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.