Results 1 to 5 of 5
  1. #1
    mrblue is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Post Plz explain "Interface"

    Hi everyone,
    I am new to java.

    Plz can you explain me the concept of Interfaces with a simple example.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Interface are abstarct by nature.It can have constants and method signatures.It can't be instantiated.But,for an interface reference variable we can assign object which implements the interface and access its functionalities.

    One main advantage of interface is multiple inheritance.

    Gothru this link below for detailed explanation
    http://en.wikipedia.org/wiki/Interface_(Java)

    Gothru this sample code for multiple inheritance

    Java Code:
    interface FirstInterface
    {
    	public void hai();
    }
    
    interface SecondInterface
    {
    	public void bye();
    }
    
    class Test implements FirstInterface,SecondInterface
    {
    	public void hai()
                {
    		System.out.println("Iam in hai method of firstinterface ");
    	}//hai
    
    	public void bye()
    	{
    		System.out.println("Iam in bye method of secondinterface ");
    	}//bye
    
    	public static void main(String[] args) throws Exception
    	{
                        //Creating a reference variable for First and second interface
    	      //and trying to access its methods defined by the class
                        FirstInterface firstInterfaceObj = new Test();
      	       firstInterfaceObj.hai();
      	       SecondInterface SecondInterfaceObj = new Test();
     	       SecondInterfaceObj.bye();
    
    	}//main
    }//class
    Last edited by RamyaSivakanth; 06-26-2009 at 04:23 PM.
    Ramya:cool:

  3. #3
    mrblue is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for that

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Interfaces can also be used when you are unsure of how you want to implement a certain procedure. If you have multiple ideas, you can implement each of them in a separate class, and then send use an instance of the class.

    Take a look at this code.
    Java Code:
    public interface MessagePrinter{
         public void printMessage();
    }
    public class Printer1 implements MessagePrinter{
         
         public void printMessage(){
              System.out.println("Do I want to print this?");
         }
    }
    public class Printer2 implements MessagePrinter{
         public void printMessage(){
              System.out.println("Or do I want to use this message?");
         }
    }
    public class MainPrinter{
    
         public MainPrinter(MessagePrinter printer){
              printer.printMessage();
         }
         public static void main(String[] args){
              new MainPrinter(new Printer1())  //this can be changed to test each class
                                              //as you figure out what you actually want. 
         }
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    448
    Rep Power
    6

    Default

    My sugg is that please refer sun tutorial about Java Fundamentals
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  3. Replies: 3
    Last Post: 12-05-2008, 08:34 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •