Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Question regarding interfaces (http://www.chiefdelphi.com/forums/showthread.php?t=78808)

isaacdl 10-30-2009 10:45 PM

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 :rolleyes:)

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

isaacdl 10-30-2009 10:52 PM

Re: Question regarding interfaces
 
1 Attachment(s)
Forgot to include the program! (attached)
Change the .txt extension to .java

Abrakadabra 10-31-2009 12:20 AM

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.

isaacdl 10-31-2009 10:59 AM

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?)

Jared Russell 10-31-2009 11:26 AM

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 ...
  }
}


isaacdl 10-31-2009 12:30 PM

Re: Question regarding interfaces
 
Ah, got it! Thanks much to both of you!

Isaac


All times are GMT -5. The time now is 07:31 AM.

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