Results 1 to 12 of 12
  1. #1
    Sayan_P007 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default What is interface?

    I want a detailed answer , in layman's term with real time examples . Please help

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You won't really get examples, but I can link you to something that shows an example of using an interface.

    An interface is an abstract class. A class in which you can't make an instance of it. It has a list of abstract methods, that must be overridden by any implementing class.

    In java there is only single inheritance, no multi inheritance like c++, interfaces allow multiple implementations.

    Simply put, an interface is a list of method signatures that a class can implement, in order to implement it however; you must provide a concrete method definition for each method.

    Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)

    Here is a blog of mine, the bottom snippet uses an interface(a very small one)
    http://www.java-forums.org/blogs/sun...recursion.html

  3. #3
    Sayan_P007 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks again, i have a few more questions related to your answers if you dont mind as im practically lost "An interface is an abstract class. A class in which you can't make an instance of it. It has a list of abstract methods, that must be overridden by any implementing class." - What do you mean by an instance of it and list of abstract methods such as ? by implementing class you mean child class right and can you please again elaborate your last line, i hope im not testing your patience

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I honestly didn't expect you to understand it all, it may be confusing, but down fret, in time it will become easier.

    An interface looks like this
    Java Code:
    interface Helper{
      int someMethod();
      double anotherMethod();
    }
    Notice that neither method has a body. When a class implements the interface with the implements keyword it is agreeing to have a full method for the methods in the interface.

    Java Code:
    public class Testing implements Helper{
      public int someMethod(){ return 42; }
      public double anotherMethod(){ return 42.0; }
    }
    When I say you can't create an instance I mean you can't use a constructor to create a new object of an interface. So this
    Java Code:
    Helper h = new Helper();
    is illegal since Helper is an interface.

    However; these are legal, these create a Testing object
    Java Code:
    Testing t = new Testing();
    Helper h = new Testing();
    The second object creation has a reference type of the interface, that's a bit out if reach for you right now so don't get too confused by it. The short version: extending a class, or implementing a class creates an is-a relationship, since Testing implement Helper, Testing is a Helper, so it's legal to have an interface as the reference type.

  5. #5
    Sayan_P007 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    One last question till i figure the rest out, again if you dont mind "Notice that neither method has a body. When a class implements the interface with the implements keyword it is agreeing to have a full method for the methods in the interface." can you show me a method with a body so that i can differentiate

    Thanks again

  6. #6
    Sayan_P007 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Java Code:
    Testing t = new Testing();
    Helper h = new Testing();
    The second object creation has a reference type of the interface, that's a bit out if reach for you right now so don't get too confused by it. The short version: extending a class, or implementing a class creates an is-a relationship, since Testing implement Helper, Testing is a Helper, so it's legal to have an interface as the reference type.
    By IS-A relationship you meant inheritance right , testing will inherit the instance variable of helper , am I right

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Close, but not quite, if you implement the interface the implementing class is a insert-interface-name-here, however; interfaces should only contain a list of methods and the implementing class must have a concrete definition for each method, so nothing is really inherited.

    Java Code:
    public int someMethod(); //no body as seen in interfaces
    public int someMethod(){
      //in body, everything from { to } is part of the body
    }

  8. #8
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I can add some remarks, but you should really take the time to study the links provided by Sunde. Whatever we explain here doesn't replace a tutorial, and there are many good ones.

    I see a class definition (the code you write) as a recipe, an instruction how to make something. An instance is the concrete result of following this recipe (the meal). An interface is a kind of contract (a recipe should describe starters, main course and desert and when followed provide you with so many calories).
    You are an instance of Homo sapiens (The class. I'm not going to spell out how to make those), and you are supposed to be able to have a good conversation with them (interface).
    Like Sunde said, in Java interfaces make multiple inheritance possible (you can't inherit from multiple classes as in C++). It is also a means to separate parts of your code in logical "loosely coupled" (not intertwined) parts. Say you make a calculator then you make the GUI part (user interface, pardon me, front end) use an interface (contract) that says that the engine (the calculating part) should be able to sum, subtract, multiply and so on. Then as a programmer you can swap engines that comply to (implement) this interface.

    Edit: I should type faster. In the meantime about five posts have past.
    Last edited by Jodokus; 06-22-2011 at 12:54 PM. Reason: Me being too slow
    No bug ever had to calculate its fitnessfunction.

  9. #9
    Sayan_P007 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I just tried doing an interface programme! showing error

    Java Code:
    package com.helpme.inf.demo;
    
    public interface Fan {
    	
    	public boolean switchOn;//the blank final field switchon may not have been initialised
    	
    	public boolean switchOff;//the blank final field switchoff may not have been initialised :- what does this error means ?
    	
    	
    
    }
    
    
    package com.helpme.inf.demo;
    
    public class GElights {
    	
    	public boolean switchOn() {
    		
    		System.out.println("Yes");
    		return true;
    	}
    
    	public boolean switchOff(){
    		
    		System.out.println("No");
    		return false;
    	}
    }
    o/p is we got a sequencer?? what does that mean ?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Put the instance variables in the class, and the method signatures in the interface.

  11. #11
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    o/p is we got a sequencer?? what does that mean ?
    What does this mean?
    No bug ever had to calculate its fitnessfunction.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,042
    Rep Power
    20

    Default

    Quote Originally Posted by Sayan_P007 View Post
    I just tried doing an interface programme! showing error

    Java Code:
    package com.helpme.inf.demo;
    
    public interface Fan {
    	
    	public boolean switchOn;//the blank final field switchon may not have been initialised
    	
    	public boolean switchOff;//the blank final field switchoff may not have been initialised :- what does this error means ?
    	
    	
    
    }
    
    
    package com.helpme.inf.demo;
    
    public class GElights {
    	
    	public boolean switchOn() {
    		
    		System.out.println("Yes");
    		return true;
    	}
    
    	public boolean switchOff(){
    		
    		System.out.println("No");
    		return false;
    	}
    }
    o/p is we got a sequencer?? what does that mean ?
    You missed the brackets () from your method declarations in your interface.
    In addition, you don't need to use the public keyword in an interface. All methods in an interface are public by default, and cannot be anything but public.

    SO:
    Java Code:
    public interface Fan {
    	
    	boolean switchOn();
    	
    	boolean switchOff();
    }

Similar Threads

  1. Interface help
    By Archer in forum New To Java
    Replies: 13
    Last Post: 04-07-2011, 11:42 PM
  2. interface
    By makrandubale in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 03:13 PM

Posting Permissions

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