Originally posted by: franguinho
Thanks that did help, and well we were told to do it this way, extending list in our queue and stack...
One thing though that i'm having trouble with: what's the difference between the actual queue and the interface? in the textbook it mentions interfaces all the time but would i even need one for what im trying to do? thx!
There are usually two uses of the word interface; often used interchangeably. When I refer to a class' interface, I refer to the interface that the consumer of your class uses. This is an entirely disparate concept from an interface construct. Consider the following class:
public class Foo
{
public int Foo1() { // implementation }
public int Foo2() { // implementation }
private int Foo3() { // implementation }
}
In the above, the class' interface consists of 2 public members which happen to be methods: Foo1 and Foo2. Note that Foo3 is not part of the interface because it is encapsulated within the class. Now, lets look at the interface construct:
public interface IFooable
{
int FooYou();
}
Now lets implement IFooable in Foo
public class Foo implements IFooable
{
// IFooable implementation
public int FooYou() { // implementation }
public int Foo1() { // implementation }
public int Foo2() { // implementation }
private int Foo3() { // implementation }
}
Now the class Foo's interface consists of 3 members, all methods: FooYou, Foo1, and Foo2. So, Foo's external interface includes the implementation of the IFooable interface. Does that make sense?
EDIT: oh and when i define clear() in the queue, does it add to the existing clear() method from the list, or does it create an entirely new one?
No, using polymorphism the method will be called on the class instance to which your variable references. Consider the following:
List l = new List();
l.clear()
That will obviously call List.clear() as your variable 'l' refers to an instance of List. Now look at the following:
List l = new Queue();
l.clear()
That will call Queue.clear() (if the override exists) as your variable 'l' refers to an instance of Queue. This is polymorphic behavior. You are able to use List to refer to an instance of Queue as Queue is a subclass of List. This is sometimes referred to as downcasting, as you are casting down the class hierarchy (from List to the subclass Queue). The same can be said for interfaces a class implements:
IFooable f = new Foo()
f.FooYou();
'f' refers to an instance of Foo because Foo implements IFooable. Note that referring to a class instance through an interface means that *only* the interface of the interface implemented will be used; in other words, you only have FooYou visible to the reference.
Hope that didn't make it too confusing.