Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain "interface" and "inner/outer"

Status
Not open for further replies.

jakhan

IS-IT--Management
Jul 22, 2000
75
I am learning java
but concept of following to terms are not clear to me
please explain by examples.

Interfaces
Inner and outer classes

thanks

Jahangir Ahmed
[sig][/sig]
 
A interface <in my understanding> is any type of device that can fire events such as, Mouse Events, Keyboard Events, threads,com ports etc.. Interfaces fire events, usually in the form of functions when a certain activity happens. for example mouse events in a applet. If you want to grab mouse movements you do not poll for events. When the event occuries, A specific function in your class is called. These functions operate independentely of your main program logic. Or you can link them to your program logic to have the interact with you progam. You will notice a depreciation of speed on events such as animation etc.. because these functions (i think) do not operate on a seperate thread.
Best way to learn would be to try to implement say some simpel animation in a applet and hook it in to the mouselistener event handler.

good luck B-)
[sig]<p>ackka<br><a href=mailto:tmoses@iname.com>tmoses@iname.com</a><br><a href= my site</a><br>"Do No Harm, Leave No Tracks"<br>
ICMP Summer 2000, 2600 Article<br>
<br>
[/sig]
 
An interface is a class, which has as members, abstract methods ONLY.

import java.io.*;

interface IFoo{
public abstract String getName();
public abstract int getInt();
}

class MyFoo implements IFoo
{
public String getName(){ return &quot;MyFoo&quot;; }
public int getInt(){ return 5; }
}

class YourFoo implements IFoo
{
public String getName(){ return &quot;YourFoo&quot;; }
public int getInt(){ return 10; }
}

public class TConsole
{
public static void showFoo(IFoo oFoo){

System.out.println(&quot;oFoo.name: &quot; + oFoo.getName());
System.out.println(&quot;oFoo.int: &quot; + oFoo.getInt());
}

public static void main (String[] args)
{
// TODO: Add initialization code here
MyFoo myFoo = new MyFoo();
YourFoo yFoo = new YourFoo();
showFoo(myFoo);
showFoo(yFoo);
}


Hope this helps
-pete
[sig][/sig]
 
basically an interface defines a way of interacting with another object it sets out what methods are available for another class to use without saying how they are to be implemented. A good example of interfaces in use is the collections api (java.util.*), some people don't teach this which is really stupid so if whoever is teaching you java doesn't introduce you to collections find someone who will (a basic tutorial is at anyway there are whole host of collections; HashSet, LinkedList, ArrayList.... they all implement methods like add(), remove(), contains(), ... but they all do it differently but as programmers we don't, and shouldn't, care. All we care about is that they do implement these methods and we can use them the same way no matter what sort of collection it is.

An inner class is a class concealed within another this is usually done when you start getting into event binding with gui stuff, although you can usually do it anywhere, for instance;
[tt]
this.addWindowListener(new WindowAdapter(){
[tab]public void windowClosing(Window event e){
[tab][tab]System.exit(0);
[tab]}
});
[/tt]
the syntax for this is a bit confusing it took me three years to get used to so for now just copy and paste stuff like this out of examples.
this is an &quot;anonymous inner class&quot; and will result in a class file called classname$number.class only the outer class classname.class will be able to access this class for the purpose of a callback for the window closing event. I haven't actually had to use anyother sort of inner class but i gather you can do it like:
[tt]
public class Outer{
[tab]//outer stuff
[tab]class Inner{
[tab][tab]//inner stuff
[tab]}
}
[/tt]
I'm sure its useful in some cases

hope this clears things up X-)
[sig]<p>Chris Packham<br><a href=mailto:kriz@i4free.co.nz>kriz@i4free.co.nz</a><br><a href= > </a><br>A thousand mokeys at a thousand machines, it happened, it got called the internet.[/sig]
 
Here's some links you can read through.

There's an interesting explanation of interface usage here. Be sure to download the examples though because the article won't make very much sense otherwise:

you should also read the tutorial at sun:

And here's another one that's nicely written I think:
[sig]<p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br> [/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top