Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Problem with Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with Interface" Watch "Problem with Interface" New topic

Problem with Interface

Padma Prasad
Ranch Hand

Joined: Sep 16, 2002
Posts: 76
I am having a problem with Interface. The Interface looks like this.
package packageName;
import java.util.*;
public interface dateIntFace {
public int getCDate();
public String getYr();
public Vector getMDates();
I have a class "dateClss" which implements this interface. It has setter functions also in addition to the getter functions of the interface.
package packageName;
import java.util.Vector;
public class Dateclss implements dateIntFace {
private int cDate;
private String yr;
private Vector m_dates = new Vector();
public void setCDate(int sysDate) {cDate = sysDate;}
public void setYr(String s) {yr = s;}
public void setMDates(Vector v) {m_dates.add(v);}
public int getCDate(){return crDate;}
public String getYr() {return yr;}
public Vector getMDates() {return m_dates;}
Now, in another class, I create objects of Dateclss, add them to a vector and store this vector in session.
I retrieve them back in a JSP custom tag class. to retrive these values, I wrote the code like this
dates = session.getAttribute("dateObj");
Iterator i = dates.iterator();
while (i.hasNext())
dateIntFace dif = (dateIntFace);
int cd = dif.getCDate();
String syr = dif.getYr();
Vector v = dif.getMDates();
// I even tried Vector v = new Vector(); v = dif.getMDates();
when I do this, the int and string values are loading but its throwing error in retrieving getMDates(). I get runtime error. If I dont typecast it to Interface and typecast it to the class type, things are working fine but i get the warning of "object not serialized" for Dateclss object.
why is it not mapping a vector? why are things working fine when retrieving int and string values and why am i getting problem while retrieving a vector.
Can an interface have imports?
Can it throw variables of collection type?
Please let me know.
[ December 01, 2003: Message edited by: N Padma ]
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
You didn't show how do you put in those vectors in your "dates". Interface cannot throw anything, and class can't too. It's methods who throw exceptions not objects or classes. Methods in interfaces and classes can throw exceptions. Regarding imports, imports are also not part of the class or interface. It's part of compilation process and they belong to file (that is why they are not part of class/interface block).
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
And you can throw anything you want but it should be derrived from Exception or RuntimeException. And pershaps you are getting error about Serializable because all exceptions are indirectly derrived from Throwable (you should derrive from Exception) which implements Serializable interface. So the problem might be because you throwing objects that are not derrived from Exception and thus do not support Seriazable interface.
Padma Prasad
Ranch Hand

Joined: Sep 16, 2002
Posts: 76
Thanks for the help, Vladas. I have known what my problem is. I am gettig the problem with Serializable. will look into it now.
Thanks again,
I agree. Here's the link:
subject: Problem with Interface
It's not a secret anymore!