aspose file tools*
The moose likes Beginning Java and the fly likes interface example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface example" Watch "interface example" New topic
Author

interface example

barclay mcdaniel
Greenhorn

Joined: Apr 18, 2008
Posts: 11
/*
can/will someone please provide me with an example of two simple .java files that i can compile and then run that uses an interface?

something like:
*/
public class Tree implements Grows{
Tree oak_A = new Tree;
// stuff suff
}

// thanks to whoever for your time/help...
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Barclay Mcdaniel,
Welcome to JavaRanch.

A very simple example can be like:




Well, but before you get into this you got to have good understanding what interface and classes are. Can you tell us what you are studying and what you tried?
barclay mcdaniel
Greenhorn

Joined: Apr 18, 2008
Posts: 11
I am a programmer hobbiest only, can work well with procedural programming- i just get lost in oop, i made this simple math program for my son (below) for simple console 6-year-old-math but want to use oop
in a file named MathWork.java for 'javac' and then runs well as MathWork.class
>>>>>>>>>>>>>>>>>>>>>>>>>>>>

barclay mcdaniel
Greenhorn

Joined: Apr 18, 2008
Posts: 11
what i was wanting with that tree/grows wording was an example that shows simple interface operations, that hello interface example did not help me see it's use/purpose, i read about interfaces lots, examples given in books and the like never show me much...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
An object presents a public "interface" to its world. An "interface" adds to that public interface, so it only has methods. [It can have constant fields, but most people think they are a bad idea.]
ExampleNow, to create an Engine class, you can "implement" that interface, whihc means that the ;s are replaced by bodies to their methods.
ExampleI have just put any old stuff in the methods, so you can see what is going on.

Notice you now have a class with methods, three of which are "specified" by the three methods in the interface. Notice that although it doesn't say anything about "public" in the interface, the methods are expected to be implemented as "public."
You can also design a DieselEngine class which also implements Engine.

You can give three names to the type of the PetrolEngine: PetrolEngine, Engine and Object. It is implicity all three at the same time.

Don't wish to blind you with science, but I hope that is of some help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

And the beauty of it all is that users of Engines don't need to know anything about the implementation. A using class just addresses the Engine interface and it could care less (and in fact doesn't even know) if the implementation is a PertrolEngine, a DeiselEngine, a SteamEngine, a SquirrelPoweredEngine or a RubeGoldbergConvolutedEngine. Because they all implement Engine, they all act like Engines.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
barclay mcdaniel
Greenhorn

Joined: Apr 18, 2008
Posts: 11
if you looked at the MathWork.java i provided, you'll see i do not know lots about oop, please provide me with a 'simple', not so-many 'engine meathods' as are there, just a simple, maybe 3 java file's code to suport like a tree class that implements a grow interface or something... that i can compile, 'javac', and run 'java' myself...

thanks for someone's time...

barclay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Originally posted by Campbell Ritchie:
. . .. . .
Example. . .
I am afraid I cannot design a SquirrelPoweredEngine for Bear Bibeault (although one type of electric motor is called squirrel-cage), but you could have an ElectricEngineNow double up the instructions in the CarDemo class so your main method now reads:Then see what happens.

And as for treesNow you can design a Lithops class where the grow method causes it to enlarge by 1mm at a time!
barclay mcdaniel
Greenhorn

Joined: Apr 18, 2008
Posts: 11
wonderful tree example, just what i was looking for...

thank you

barclay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface example