Results 1 to 12 of 12
Thread: What is interface?
- 06-22-2011, 11:30 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-22-2011, 11:49 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 06-22-2011, 12:00 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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
- 06-22-2011, 12:14 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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:interface Helper{ int someMethod(); double anotherMethod(); }
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 thisJava Code:public class Testing implements Helper{ public int someMethod(){ return 42; } public double anotherMethod(){ return 42.0; } }
is illegal since Helper is an interface.Java Code:Helper h = new Helper();
However; these are legal, these create a Testing object
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.Java Code:Testing t = new Testing(); Helper h = new Testing();
- 06-22-2011, 12:19 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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 .gif)
Thanks again
- 06-22-2011, 12:25 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-22-2011, 12:31 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 }
- 06-22-2011, 12:35 PM #8
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.
- 06-22-2011, 12:49 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
I just tried doing an interface programme! showing error
.gif)
o/p is we got a sequencer?? what does that mean ?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; } }
- 06-22-2011, 12:53 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Put the instance variables in the class, and the method signatures in the interface.
- 06-22-2011, 01:10 PM #11
What does this mean?o/p is we got a sequencer?? what does that mean ?No bug ever had to calculate its fitnessfunction.
- 06-22-2011, 01:14 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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
-
Interface help
By Archer in forum New To JavaReplies: 13Last Post: 04-07-2011, 11:42 PM -
interface
By makrandubale in forum New To JavaReplies: 9Last Post: 01-02-2011, 03:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks