• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor question

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do meen

"its defined as intance"

what does it meen??
for me when i am using a constructor

i am making

code:


SameName t=new SameName();



but here in this example

code:


Stack s = new StackAsArray();



Stack is an interface
which meens nothing because i got no code.
and after the word "new" we dont get "Stack"
but "StackAsArray".
i cant understand that.

and how did they use StringTokenizer
because when i tried to wright as they do
it says that there is no such type
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already asked and been given an answer about StringTokenizer. Have you applied that advice? BTW: If you read the API for StrinkTokenizer it is described as legacy code and they tell you what to use instead.

You have also been given an explanation of using interfaces as types.
The principle is called "program to the interface" and the earliest use of it I have seen is in the Gang of Four book (E Gamma, R Helm, R Johnson, J Vlissides: Design Patters Elements of Reusable Object-Oriented Software. Reading MA: Addison-Wesley 1995). It means:You can then put anything in that space which has those methods and has an interface name Stack (spelt "implements Stack").

You can then fit your ArrayStack or whatever it is called. Should you wish to change it to a VariableArrayStack or a LinkedStack, that is really easy.You have one declaration and different implementations in that snippet of bad code.

Have you not been told to declare your Stacks as generic types? It would be far better if you could use a Stack<Character> which would save you all the problems you are having with casting.
 
johny doe
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so my variable takes only the methods which are
mentioned in the Stack interface
from StackasArray class

and puts them into the variable
what about public variables that where written in the class
are they included too
??
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other variables written in the Stack: no, they are not included.

But a Stack is a class which has these methods:
  • push
  • pop
  • peek
  • isEmpty
  • and it throws an EmptyStackException if you try to get anything out when it's empty.
  • You might have a peekSecond or size method too, if you so wish.
  • But it doesn't have any other variables. That is all you expect from a Stack. You don't expect to find other variables in it.

    You can have a Car interface like this
    Then you can have a Ford class or a BMW class or a Mercedes class or even an AbstractCar class whihc implements these methods. Those are all the things cars do. They use fuel, they go in different directions at different speeds. The interface has no need for methods like public void goShopping() or variables like private int temperature. Those things are to be taken care of by different classes. There might be a Thermometer interface which takes care of temperatures.

    And you shouldn't have public variables in your classes in the first place. (Except public static final fields.)
     
    johny doe
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so its only the methods which are mentioned int the interface
    ??
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When you declare an object to be of Stack type, when Stack is an interface, the Compiler will only look for the methods in the Stack interface. It will ignore any other methods, and no you can't use them as things stand.

    Look at the API documentation for the java.util.List<T> interface. Lists are random-access collections. Then go and find some classes which implement it, the best known being java.util.ArrayList<T> and java.util.LinkedList<T>. Count how many methods they have, and how many are in the List interface.
    Then find the java.util.Queue<T> interface and find out how many of its methods there are in the ArrayList and LinkedList classes. I think it's probably none in ArrayList and all in LinkedList.

    You can sayThen you are using the methods of List<XYZ> but it is an ArrayList<XYZ> which implements them. You can write this too, and have the same methods implemented by a LinkedList<XYZ>:In fact I used ctrl-C and ctrl-V to create that last code snippet. Both cases have the same methods and the same functionality, but a different implementation. Look in the Java Tutorial for collections. Back to ArrayList: there is one method called ensureCapacity or something similar which is in ArrayList and not in List. That is an implementation-specific method, which is very valuable for ArrayLists but would be useless for LinkedLists. It enlarges the collection so you can add many more members very quickly; you can't do that with a LinkedList. Should you wish to use it, it looks like this:Note the class cast, with a second pair of round brackets () because the . operator has a higher priority than casting. Also this is one place where instanceof is really useful; you can only cast an ArrayList to an ArrayList.

    You can also declare a Queue (first-in-first-out). A useful way to implement a Queue would be with a LinkedList. So, here goes:See, a completely different set of methods, but the same class implementing it. Now tryLinkedList has it, but it's not in the Queue interface, so the compiler won't recognise it and won't compile the code.

    I am afraid I haven't found an easy explanation of "program to the interface" by using Google. Anybody else able to help?

    [edit]Disable "smilies"[/edit]
    [ January 04, 2008: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by johny doe:
    so its only the methods which are mentioned int the interface
    ??

    Yes, as I have tried to explain a minute ago.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic