LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2003, 04:14 AM   #1
gwp
Newbie
 
Registered: Oct 2003
Location: South Africa
Distribution: Redhat, Fedora, Ubuntu
Posts: 27

Rep: Reputation: 15
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
 
Old 12-08-2003, 07:14 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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?
 
Old 12-09-2003, 04:37 AM   #3
memory_leak
LQ Newbie
 
Registered: Dec 2003
Posts: 24

Rep: Reputation: 15
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

Last edited by memory_leak; 12-09-2003 at 05:07 AM.
 
Old 12-09-2003, 04:25 PM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Inner Classes KissDaFeetOfSean Programming 1 07-29-2005 03:50 PM
using custom java classes in perl eastsuse Programming 1 12-02-2004 05:41 PM
compile java classes to binary? ludeKing Programming 2 08-15-2004 09:59 AM
Java action events AMMullan Programming 22 04-16-2004 04:57 PM
importing my own classes in Java vanquisher Programming 9 10-02-2003 03:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration