|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Re: Question regarding interfaces
Forgot to include the program! (attached)
Change the .txt extension to .java |
|
#3
|
|||
|
|||
|
Re: Question regarding interfaces
As the Java Tutorials say:
Quote:
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. |
|
#4
|
|||
|
|||
|
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?)
|
|
#5
|
|||||
|
|||||
|
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 ...
}
}
|
|
#6
|
|||
|
|||
|
Re: Question regarding interfaces
Ah, got it! Thanks much to both of you!
Isaac |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |