• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

collection interface and other interfaces

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have to implement every method in an interface.
But in collection interface some of the methods are optional ie, a given implementation may not support some of this options.How this can be?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anuji,
A non abstract Class that implements an interface, must provide a body for every method of the interface
I supposed that your question, is because in the API documentation from Sun of the Collection Clase, there are several methods where Sun said that are optional (optional operation)

For example, the method add() is optional, but you have to implement it. If the class that u are creating, not support this method, you will have to throw an exception (UnsupportedOperationException), when the method is called.
 
Anuji Philip
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean like this
for a method of interface;

void method()
{
throw new UnSupportedOperationException();
}
then if in jdk they didn't mark "optional" too we can do it.
Where I am wrong?please explain
 
Alf Fernandez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... well, i will explain you with an example...
Suppose I create an interface, named Vehicle, that define the behavior that the clases implement it, could have.
public interface Vehicle{
void openDoors();
void turnOn();
// more methods
}
Then I write a documentation, explaining that the methods should do. In the 'openDoors()' method, I say: 'this method is optional', because there are vehicles, that doesn�t have doors
Now, you create two clases, implementing the interface: 'Car' and 'Bike'
public class Car implements Vehicle{
void openDoors(){
//some stuff here
}
void turnOn(){
//some stuff here
}
}
public class Bike implements Vehicle{
void openDoors(){
throw new UnsupportedOperationException();
}
void turnOn(){
//some stuff here
}
}
In the Bike class, the method 'openDoors()', heve no sense, since the bikes have no doors. But because the Java languaje rules, you have to implement every method of the interface, so in the Bike class you throws an exception, while in the Car class, does something.

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I am not very sure if the given example could be desirable from an Object Oriented Dessign point of view. I mean having a type (interface) whose -is a- related types (implementing classes) can not respond to some of the messages defined in the type (optional methods).
Bruce Eckel guru tell us in Thinking in Java the reason why Sun's people took that approach in the collection framework: to avoid an explotions of types. For instance the object that the method Arrays.asList() returns is a List, however it is really an especial one because the optional methods that modify the number of elements are not implemented. The reason is that the returned List is backed up by an array, thus changes to its size must no be allowed. Without the optional methods approach, the type of the returned object should be something like FixedSizeList; a new one that doesn't exist in the API.
I mean that you should have a good reason for having optional methods in interfaces.
(Note: all the classes in the framework implement the optional methods)
 
Alf Fernandez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose,
Por la respuesta que das, no se si es que no te has leido la pregunta que hacia anuji, o es que estas contestando a otra distinta
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alf,
Sounds like spanish, what does it mean?
-Arun
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He belives I haven't read the question.
I still think the reply was appropiate. What do you think?
No clear enough? no relevant?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't seem to make much sense to me to make a Bike a Vehicle when a Vehicle promises to openDoors() and turnOn(), while a Bike can't. Isn't a Vehicle supposed to represent a certain commonality? An interface or an abstract class is supposed to be general but isn't this going a bit too far?
The implemented methods in the collection classes on the other hand were intended to be very specific convenience methods, providing alternate views of the same object (seen as a List, seen as synchronized, seen as immutable, etc.) which justifies their use.
-anthony
 
Alf Fernandez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well.... you know .... an example is only an example ....
 
Alf Fernandez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and ... if Anuji understood the answer .... that is a '''GOOD EXAMPLE'''
 
Anuji Philip
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have to say,JOSE BOTELLO is the best guru
java study forum got.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't confuse people . I am not a guru. Honestly I don't think I will be a guru in the future either. Yes, I would like to become a Java programer one day.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic