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 5 of 5

Thread: Interface

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Hi can you help me (i need yours help) please

    interface a
    {
    void display();
    }
    interface b
    {
    void display();
    }
    class ab implements a,b
    {
    public static void main(String arg[])
    {
    ab c=new ab();
    display()
    {
    System.out.println("Which interface will cal at this time");
    }
    }
     
    }


    can you tell me what is the out put
    and which interface will call when you want display ()
    Last edited by helloworld922; April 21st, 2011 at 09:56 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Interface

    No. Can you tell us? What happened when you wrote a test program to test this?
    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
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Interface

    Assuming that's all the code, neither. You'll get compile errors.

    If you're unsure what code's doing, test it out.

    Note that an interface can't be "called", neither can a method in an interface be "called" (since all interface methods are abstract). Only the lowest implementation of that method (with respect to the actual object type) is called.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Interface

    Also, this is what is referred to as "the diamond problem". (If you have no idea what that is, google it and come back.)

    The diamond problem *usually* doesn't happen in Java, because there is no multiple inheritence. However, as you've shown here, you can implement multiple interfaces.

    An interface doesn't care how you implement a method, as long as it's there. So it doesn't care if another interface also contains that method definition. It's up to you as a programmer to know what's going on.

    Whether or not this is the diamond problem is debatable. Wikipedia says it isn't. But it can be confusing, especially if you want two functions to do completely different things. For example:

    public class DiamondProblemTest {
     
    	public interface Cowboy{
    		public void draw();
    	}
     
    	public interface Artist{
    		public void draw();
    	}
     
    	public static class Person implements Cowboy, Artist{
    		public void draw(){
    			//should I pull out a gun or a paintbrush?
    		}
    	}
     
    	public static void main(String... args){
    		new Person().draw();
    	}
    }
    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!

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Interface

    The answer's obviously that your person needs to draw pictures with his bullets

Similar Threads

  1. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  2. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  3. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM
  4. Interface problem please help!!
    By joff1403 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:09 AM
  5. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM