Few java questions

edited September 2003 in Internet & Media
I'm supposed to make a console program that takes 2 rational numbers and does various things to them. my question is this, my prof gave us an 'interface' Rational to implement into our MyRational class. My question is what is the point? all the functions in the interface have to be retyped into the main class myrational. Is there something i'm missing? And in the interface for the data type he Puts Rational as the data type, but it's an interface and can't be an object? Any help is appreciated! :)

Comments

  • a2jfreaka2jfreak Houston, TX Member
    edited September 2003
    I think I'll let Sun describe it.

    http://java.sun.com/docs/books/tutorial/java/concepts/interface.html



    What Is an Interface?

    In English, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks. Within the Java programming language, an interface is a device that unrelated objects use to interact with each other. An interface is probably most analogous to a protocol (an agreed on behavior). In fact, other object-oriented languages have the functionality of interfaces, but they call their interfaces protocols.

    The bicycle class and its class hierarchy defines what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn't care what class of items it manages as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of constant and method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.

    To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on.

    You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following:

    * Capturing similarities among unrelated classes without artificially forcing a class relationship.
    * Declaring methods that one or more classes are expected to implement.
    * Revealing an object's programming interface without revealing its class.
Sign In or Register to comment.