aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I can't find a good explantion for this code anywhere.. Please help me..

interface cpu {...}
interface keyboard {...}
class computer implements cpu, keyboard {...}

what does this line actually mean:

keyboard k = new computer();


Thanks guys..
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
each computer instance you create is of type "Computer", right?

since your class implements Keyboard interface, computer instances will also be of Keyboard type

So, your k instance "is" 3 types: Computer, Keyboard and Cpu.

You can code like this:
List myList = new ArrayList();
meaning myList "is" a List (in this precise case an ArrayList)

in your post is exactly the same:
keyboard k = new computer();


java amateur
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

interfaces in java are contracts. if i write an interface, i am defining the terms of the contract. if you implement the contract, you agree to fullfill the terms.

now, what are those terms? basically, they are methods. in the interface i write, i'm saying "i don't care what ELSE you do, but you MUST implement these methods with these signiatures".

So if you write

class XXX implements fredsInterface {...};

you are saying "yes, this class will implement the methods defined". note that it doesn't matter HOW you implement them, they can even be empty, as long as they are there, take the right parameters, and return the right thing.

now i know you're gonna ask "WHY would i do this?"

well, look at the code you posted. the computer class can actually now be thought of as three different things. 1) it's a computer. 2) it's a keyboard. 3) it's a cpu.

what this really lets me do is put a bunch of objects into a collection, and treat them all as keyboards. i can put in a computer, a type-writer, an adding machine, or anything else, as long as they implement the keyboard interface.

when i pull them out, i don't even CARE what they are, specifically. i can treat them all as keyboards, and thus can call any of the keyboard methods on them.

in fact, i can write my code to deal with keyboards. then, 5 years later on, somebody can come up with SOMETHING I'VE NEVER HEARD OF, implement the keyboard interface, and stick it into my code, and i know it'll run just fine - they agreed to my contract, so i can call my methods on them.

So, your actual question about your actual line of code - you're creating a new computer. but it implements the keyboard interface, so i can treat it as a keyboard, and call the keyboard methods.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
When you declare a class, indicating that it implements one or more interfaces, you can assign an instance of it to a variable declared as any of the interfaces it implements.

So, you "computer" object can be viewed by other client code as a keyboard, or a cpu, and as a computer!!!

You can look at it as an instance of class implementing interfaces has the behavior of its interfaces.

Although I admit that the code you showed sounds illogical: a computer "is not" a keyboard, but it has a keyboard.

That code works, but it would be more logical to have something like:

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
So when I say:

keyboard k = new computer();

can I say:

k.some_method();
k.some_value;

Can I refer like this as long as some_method() and some_vlaue is defined in keyboard interface?
Using "k" instance is it only mehods and variables of keyboard interface that I can call?

Thanks a lot guys... :-)
Alan Jump
Greenhorn

Joined: May 25, 2005
Posts: 26
Originally posted by Maduranga Liyanage:
[QB]
Can I refer like this as long as some_method() and some_vlaue is defined in keyboard interface?
Using "k" instance is it only mehods and variables of keyboard interface that I can call?
QB]


The interface provides a set of constructors only...if you implement the interface, it's up to you to write the methods that the constructors refer to. So if the interface specifies a constructor:

...you have to write the body of some_method() in the class that implements the interface:


What I'm not sure of is if an interface can specify a variable value in its constructor; neither of my texts (Deitel & Deitel and Head First Java) make note of that. Can someone answer that small question for me? I'm sure I'll see it again sometime...
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Maduranga, yes, you can refer to the implemented methods of an interface as you wrote. You can also have access, through the dot notation, to the interface constants but of course you would not be authorized to change them. You can only access the interface methods and constants, no others. You can also (and it is preferable) access the constants directly through the interface name with the constant name, like


For some clarification: interfaces do not define constructors. Constructors belong to classes only.

Here is an example:
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
In one of the examples given in the book "Teach yourself Java in 21 days" has put a value to variables. But I'm not sure what you exactly mean by in the "constructor"? Are there constructors like for classes in interfaces?

One more thing;
What is the purpose of defining:

keyboard k = new computer(); rather than defining;
computer c = new computer();

Please tell me what I can exactly do and cannot do with the "k" reference?

Thanks..
Alan Jump
Greenhorn

Joined: May 25, 2005
Posts: 26
Originally posted by Serge Plourde:

For some clarification: interfaces do not define constructors. Constructors belong to classes only.


Ah...incorrect choice of terminology on my part, but an important difference. Classes have constructors, interfaces have abstract methods.

<-- /me hammers fact into brain

(I'm betting I see that on an exam sometime soon...but I digress.)

The main point is that a class that implements an interface must define all methods contained in that interface, and if a variable is defined in the interface it will be a static final. Did I miss anything?
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Serge.. It clarified me a lot of things.. Thanks for the help fellows..
Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 43
Hi Maduranga,
Yes you can do that only when you have those things in ur
interface.

Regards
Waez Ali
[ June 01, 2005: Message edited by: Waez Ali ]
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Alan Jump:
a class that implements an interface must define all methods contained in that interface


False A class that implements an interface can implement some, but not all of the interface's methods, if that class is declared abstract. Of course, there will come a time that some subclass, somewhere down the inheritance tree, will have to implement the rest of the methods still to implement!

Please, don't hit your head on the wall like that. We all learn!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces