• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface example

 
barclay mcdaniel
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
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
Posts: 341
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
barclay mcdaniel
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wonderful tree example, just what i was looking for...

thank you

barclay
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic