LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java - Interfaces, inner classes and callbacks for events (https://www.linuxquestions.org/questions/programming-9/java-interfaces-inner-classes-and-callbacks-for-events-122571/)

gwp 12-04-2003 04:14 AM

Java - Interfaces, inner classes and callbacks for events
 
Hi

I'm trying to teach myself java....

I've been going through Thinking in Java 3rd Ed (Bruce Eckel) and it's been a great
help.... until Chapter 8 ....I must admit I'm lost!

I'd like to know if there is any good tuts or other material on the net that can help

Chapter 8 covers Interfaces and its use for implementation hiding and multiple inheritance as well as inner classes and how to use them with interfaces to create callbacks....

My head is aching!

I'm honestly not grasping this... so any other references and tutorials would be most welcome

Thanks

BR

Greg

coolman0stress 12-08-2003 07:14 PM

I can't think of any links right now, but if you have any particular questions, just ask away. I'm sure a good discussion can help you out a bit.

So what is it that you're having problem with?

memory_leak 12-09-2003 04:37 AM

why don't you check www.java.sun.com

check their tutorial; it's very good for beginners

then you can check: www.javaworld.com, they have a lot of tutorials too; then just write java tutorial in google and you will get thousand of sites:

About interfaces - it's really very easy. It's something very common to bject oriented languages and is oftern called "implementing by contract" (or someting like that).

IN java an interface is just declarations and can only contain public visible declarations. It is what is said: usual example they throw in books is to thing about for example video player: Interface is buttons and controls that you can see. You don't care so much how they are implemented inside the box; but just about what they do (for example volym will play it louder or less loud).

In java interface is much like that. You declare an interface and put some methods in it, and then when you are working with an class that implements that interface you know that this class implements methods in that interface, You don't care in principle how and what other methids are there; only those declared in interface. For example:

interface Player{
public void play();
public void stop();
}

class VideoPlayer implements interface Player{
public void play(){
( do someting here )
}

public void stop() {
(do something ihere )
}

public void anotherMethod()
{
....
}
} // end vidoe player

Now you can declare an instance of video player class. If you declare instance
as reference to your Player interface only methods visible are those declared in interface Player; if you declare an instance of VideoPlayer then you can call even anotherMethod:

class test
{
main() // just to be short ....
{
Player p = new VideoPlayer(); // only avialable methods are play & stop
VideoPlayer p2 = new VideoPlayer() // avialable play, stop & anotherMethod
}
}

There are some technical details to remember about interfaces but they are really not difficult:

First when you use interfaces, if you declare a method in interface you MUST implement that methods (or declare an abstract class). (Remember of interface as contract that obligate you).

Second you can't make an instance of an interface, since an intrface doesn't contain any implementation of methods, only declarations. It's pretty much like abstract methods. For example you can't write:

VideoPlayer p = new Player();
or
Player p = new Player();

But you can declare refernce to an interface or bettter to say reference to an object that implements certain interface; concrete:

Player p = new VideoPlayer();

Here you tell to compiler to make an VideoPlayer object, but that you are interested only in methods that are declared in Player interface.

About multiple inhertiance; such things doesn't exist in java. Some people refer to interfaces as some kind of replacment for it; but it really isn't. Maybe on conceptuall level, but not on practical, since interface can contain only declaratations of methods.

You can also use interface to cheat a bit with constants and global things. Since in java everything is local to some class, there is no global visible things; unless they are static. Putting variables in interfaces and declaring thea as public static is an easy way to get them global visible (if noeeded for some reason).

About thinking in java. I sow that book since 5 years back or something but I never liked it too much; I do like idea of public books, but really referring to all examples as class A, Class B, Class c1 class c2 and such is not really pedagogic. Somewhere classes become too many and you loose track of what was class A what was class B etc. I prefer in teaching examples to see a bit more creativ (and descriptiv) names that are easier to remember.

When I was learning java at university 3 years back, we were usings David J. Barnes: Object-Oriented Programming with Java. I liked book, coz' it really explained well object-oriented conepts, it had a litlle runnable example to every concept it was explaining and is written in nice language. I remember that even sa begginer at that time I relized that it was much better book than thinking in java and worth money.

hope it helps

coolman0stress 12-09-2003 04:25 PM

There have been many revisions of the book. It got published. Bruce Eckel thought it was also good to just make it available to anyone online. The versions that you can find are also revisions, so they are or were a work in progress.

The last release is just an amazing book. Better than many intros.


All times are GMT -5. The time now is 12:24 AM.