Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Declaring an object using an interface implemented by its class vs declaring an object using the class

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Declaring an object using an interface implemented by its class vs declaring an object using the class

    Suppose you have a generic Dog class of the pet type that implements a DogInterface of the pet type. What is the difference between;

    DogInterface<pet> Rex = new Dog<pet>();
    and
    Dog<pet> Tye = new Dog<pet>();

    In what situations might you want to use Rex instead of Tye?
    Last edited by pyler; April 9th, 2014 at 04:37 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Declaring an object using an interface implemented by its class vs declaring an object using the class

    This is the same as having a List<String> interface and an ArrayList<String> implementation.

    You use this:

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

    Instead of this:

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

    Because you don't care whether myList is an ArrayList, as long as it's a List. If later you change myList to be a LinkedList instead of an ArrayList, the second line would require code changes, and the first line would not.

    This is called "programming to an interface", and it's worth a google.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Declaring an object using an interface implemented by its class vs declaring an object using the class

    Adding to what Kevin said. Here is the most common advantage, in my experience.
    Let's say we had the DogInterface, and then two classes which implement the DogInterface: Dog and Puppy.
    Declaring a list with the DogInterface type allows us to add both Dog and Puppy objects to the same list. Also, if we are unsure what subclass of a type we will use, we can declare the variable with whatever superclass our options share.
    Here is a code example which covers a bunch of different uses for this sort of stuff:
    public class Example {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		/* dogs is a list of DogInterface types because it
    		 * can contain BOTH Dog and Puppy objects
    		 */
    		List<DogInterface> dogs = new ArrayList<>();
    		while(true) {
    			/* rex is of type DogInterface because we do
    			 * not know if the user will create a new
    			 * Dog object or a new Puppy object
    			 */
    			DogInterface rex = null;
    			System.out.println("Enter 'Dog' or 'Puppy'");
    			String input = in.nextLine();
    			if(input.equals("Dog")) {
    				rex = new Dog();
    			}
    			else if(input.equals("Puppy")) {
    				rex = new Puppy();
    			}
    			if(rex==null) {
    				break;
    			}
    			dogs.add(rex);
    		}
    		for(int i=0;i<dogs.size();i++) {
    			/* dog is of type DogInterface because we 
    			 * do not know if we are getting a Dog or
    			 * a Puppy object from the dogs list
    			 */
    			DogInterface dog = dogs.get(i);
    			/* We use instanceof to determine if the
    			 * DogInterface is "really" a Dog object
    			 * or a Puppy object
    			 */
    			if(dog instanceof Dog) {
    				System.out.println("A dog");		
    			}
    			else if(dog instanceof Puppy) {
    				System.out.println("A puppy");
    			}
    		}
    	}
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] Need help, Object Oriented / Class & Object.
    By jedmustdie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2014, 09:21 PM
  2. What is the difference in declaring class variables?
    By Robertgif in forum Java Theory & Questions
    Replies: 3
    Last Post: September 18th, 2013, 12:52 AM
  3. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  4. Convert File Object to class<?> object
    By CEO in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 27th, 2012, 06:55 PM
  5. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM

Tags for this Thread