Go to Post But should we "discriminate" based on how long your team has been around? If you think about it, FIRST's goal is to inspire students, right? ...But students on rookie and second-year teams, if anything, are in greater need of being inspired. - LauraN [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 30-10-2009, 22:45
isaacdl isaacdl is offline
I have a white hat..it's just dirty
AKA: Isaac Dontje Lindell
FRC #3018 (Nordic Storm)
 
Join Date: Oct 2009
Rookie Year: 2010
Location: St. Peter
Posts: 32
isaacdl is an unknown quantity at this point
Question regarding interfaces

Hi,

I'm trying to teach myself Java for this years competition, having never done anything more complex than Liberty Basic. I understand classes, and how to use one to define an object. I'm using the Java tutorials on Sun.com to teach myself. I got to the section on interfaces, and for the life of me I cannot understand how to use them, or even what they are.
I have this sample program (attached), which uses bicycles as an example class. There is the superclass Bicycle, and the subclass MountainBike. 3 objects are defined, bike1, bike2, and bike3. Bike1 and bike2 are normal Bicycle objects, and bike3 is a MountainBike.

Bicycle has states "cadence", "speed", and "gear". It has methods "changeCadence", "changeGear", "speedUp", "applyBrakes".

MountainBike extends class Bicycle to include state "awesomeness" (first state that popped to mind )

At the end, the program prints the states of each bike.

How would I use interfaces in a useful manner in this program? What use are they in general?

Thanks,
Isaac
Reply With Quote
  #2   Spotlight this post!  
Unread 30-10-2009, 22:52
isaacdl isaacdl is offline
I have a white hat..it's just dirty
AKA: Isaac Dontje Lindell
FRC #3018 (Nordic Storm)
 
Join Date: Oct 2009
Rookie Year: 2010
Location: St. Peter
Posts: 32
isaacdl is an unknown quantity at this point
Re: Question regarding interfaces

Forgot to include the program! (attached)
Change the .txt extension to .java
Attached Files
File Type: txt bike app.txt (1.8 KB, 41 views)
Reply With Quote
  #3   Spotlight this post!  
Unread 31-10-2009, 00:20
Abrakadabra Abrakadabra is offline
Here We Go !!!
AKA: Scott Kukshtel, Mr. K
FRC #3467 (The Windham Windup!)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Windham, New Hampshire
Posts: 160
Abrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant future
Re: Question regarding interfaces

As the Java Tutorials say:

Quote:
Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Basically, a Java Interface would be used by higher level code in place of a Class name when referring to an object that it is using. By "contract", any class object that implements that Interface will be required by the compiler to provide certain methods that take certain particular arguments and return certain particular values (i.e. the methods are all required and they all have well-defined "signatures"). This will allow the higher-level code to not worry about the internal implementation of a class, and instead can focus on just using that class via its "standard" methods.

If you later want to replace the lower-level class (or better yet, if it gets replaced for you down inside a library), your higher-level code doesn't care, because it just continues to use the same Interface.

This is just another expansion of the concept of "object abstraction" that got its more commonly known start with "function prototypes" in C++. Look them up for a possibly simpler explanation. Java Interfaces are essentially function prototypes applied to the entire class.

HTH.
Reply With Quote
  #4   Spotlight this post!  
Unread 31-10-2009, 10:59
isaacdl isaacdl is offline
I have a white hat..it's just dirty
AKA: Isaac Dontje Lindell
FRC #3018 (Nordic Storm)
 
Join Date: Oct 2009
Rookie Year: 2010
Location: St. Peter
Posts: 32
isaacdl is an unknown quantity at this point
Re: Question regarding interfaces

So do I have to define both a class and an interface to use the interface. (i.e. Do I make the Bicycle class, then also the Bicycle interface?)
Reply With Quote
  #5   Spotlight this post!  
Unread 31-10-2009, 11:26
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Question regarding interfaces

There are three main types of constructs in the Java world: abstract classes, concrete classes, and interfaces.

Abstract classes have at least one abstract method - this means that you cannot instantiate an object of an abstract class. For example, a "Vehicle" abstract class might have an abstract method "move()" - all vehicles can move, but they do it in different enough ways that you can't just make a Vehicle object and call move() - there's not enough information for that to be meaningful. Abstract classes can provide default implementations of methods, as well.

Concrete classes have no abstract methods, but they can inherit from abstract classes and provide implementations of abstract methods from the parent class. For example, a "Bike" class might inherit from "Vehicle" and provide a "move()" method. Now any function that takes a "Vehicle" class as an input can also take a "Bike" class and call the "move()" method, because it knows that all Vehicles can move. This is called polymorphism.

Interfaces are very similar to abstract classes - you can't instantiate them, only implement them. For example, a Bike might implement a "WheelieCapable" interface, since Bikes can do wheelies. WheelieCapable provides a "wheelie()" method that Bike must not implement. The difference is that while a concrete class can only inherit from one abstract class, they can implement MANY interfaces.

Code:
// Abstract class
public abstract class Vehicle
{
  // Abstract method
  public abstract void move();

  // Concrete method that can be overridden
  public void getIn()
  {
    ... do something ...
  }
}

public interface WheelieCapable
{
  public void wheelie();
}

public class Bike extends Vehicle implements WheelieCapable
{
  // Override the abstract method
  public void move()
  {
    ... do something ...
  }

  // Don't override the getIn() method - just use the default implementation

  // Implement the interface method
  public void wheelie()
  {
    ... do something else ...
  }
}
Reply With Quote
  #6   Spotlight this post!  
Unread 31-10-2009, 12:30
isaacdl isaacdl is offline
I have a white hat..it's just dirty
AKA: Isaac Dontje Lindell
FRC #3018 (Nordic Storm)
 
Join Date: Oct 2009
Rookie Year: 2010
Location: St. Peter
Posts: 32
isaacdl is an unknown quantity at this point
Re: Question regarding interfaces

Ah, got it! Thanks much to both of you!

Isaac
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
question regarding driverstation ringo115 Programming 4 16-02-2009 11:16
Question Regarding Torque - Pulling CollmandosGrl Technical Discussion 2 31-01-2008 17:56
Simple question regarding OI LED's Tom Line Programming 3 29-01-2008 12:03
Question regarding to Battery Packaging sxlterps General Forum 1 19-02-2005 17:37
Question regarding Florida archiver 2001 1 24-06-2002 03:00


All times are GMT -5. The time now is 11:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi