0

For example I have:

    public class Class1 implements Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    public class Class2 implements Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

Now I want to make an ArrayList of those two types. This is what I'm trying:

List<Serializable> list= new ArrayList<Serializable>();

I have a method that returns Serializable, which returns the object in the list:

  public Serializable get(int i)
{
    return list.get(i);
}

However, when I'm trying to use the getter method from the two classes above( something like list.get(0).getA()) , there's an error saying the getA() method is not defined for Serializable. Am I missing something simple here? What should I use for the return type of the method above in order to use the getter method? Thanks.

3
  • 1
    You need another interface with the getA() method that both of these types implement. Commented Mar 27, 2014 at 16:39
  • 1
    Serializable doesn't actually have any method named getA(). Maybe you should try another approach more coherent with what do you want (maybe both classes implement another interface that did have a getA() method and having a List of that interface instead of Serializable) Commented Mar 27, 2014 at 16:42
  • You probably don't want to be using Serializable like that. As @Averroes said, create another interface which extends Serializable, and use that for your list. Commented Mar 27, 2014 at 16:45

7 Answers 7

5

Both those classes are Serializable but Serializable doesn't have any methods defined.

You will need to define either a base class or an interface yourself that contains the getA() method and then you can reference them using that.

Remember that while in this case the only Serializable classes you have are Class1 and Class2 but in most cases you cannot guarantee that. There could be another Class3 that implements Serializable but does not have the method getA.

Sign up to request clarification or add additional context in comments.

Comments

3

You are returning a Serializable object, not a Class1 object. There are two routes to doing this, but I recommend the latter:

Serializable s = list.get(1);
if (s instanceof Class1) {
    Class1 clazz = (Class1) s;
    clazz.getA();
} //etc...

Or, using a common interface:

public interface YourClass extends Serializable {

    public int getA();

}

//...

List<YourClass> list = new ArrayList<>();

Comments

1

This approach uses a interface that have a method named getA(). If both your classes implements this interface you will be able to call that method from a List of the interface type.

 public interface MyInterface {
      public void getA();
    }

 public class Class1 implements MyInterface, Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    public class Class2 implements MyInterface, Serializable  {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    List<MyInterface> list= new ArrayList<MyInterface>();

This way you can do list.get(0).getA();

3 Comments

None of those classes are serializable any more (assuming that it is a requirement for them to be). Either you need to implement Serializable on each class, or make MyInterface extend Serializable.
@JonK Yes. I didn't use Serializable because he didn't say he needed them to be. Fixed it nonetheless. Thanks ;)
It's difficult to know whether it's a case of Serializable being repurposed for something that it wasn't intended for, or if it's being used as a shortcut when it shouldn't be - I was assuming that Serializable was a requirement
0

Serializable interface in Java doesn't have any methods it's just a marker interface. If you want to have a common method you should create another interface with getA() and than implement this interface in your both classes.

Comments

0

You should create a interface containing the method getA(). Your list should have your interface as generic type.

public interface MyInterface
{
  public int getA();
}

public class Class1 implements MyInterface
{
  public int getA()
  {
    // Your implementation of getA()
    return 0;
  }
}

Now create somewhere in your code 2 objects of type Class1 and put the object in a List of generic type MyInterface:

List<MyInterface> myList = new ArrayList<MyInterface>();

Class1 obj1 = new Class1();
Class1 obj2 = new Class1();

myList.add(obj1);
myList.add(obj2);

Now you can iterate over your list and get objects of type MyInterface (instead of Serializable) so you can call method getA on the objects.

Comments

0

Create an interface that declares the methods common to all of your classes.

interface A extends Serializable {
   public int getA();
}

public class Class1 implements A {
   private static final long serialVersionUID = 1L;
   private int a;
   private int b;

   /*Getters and setters here*/
   public int getA();
}

public class Class2 implements A {
   private static final long serialVersionUID = 1L;
   private int a;
   private int b;

   /*Getters and setters here*/
   public int getA();
}

Then declare a List of A's:

List<A> mylist = new ArrayList<A>();

you can do your method if you want (but its not adding much):

public Serializable get(int i) {
   return mylist.get(i);
}

mylist is declared to have A objects which all implement the getA() method.

Comments

0
 Try this...

 List<Class1> list= new ArrayList<Class1>();
    Class1 c = new Class1();
    c.setA(2);
    c.setB(4);
    list.add(c);
    System.out.println(list.get(0).getA());

This will print 2

and

List<Serializable> list2= new ArrayList<Serializable>();
list2.add(c);
System.out.println(((Class1)list2.get(0)).getA());

This will also print 2

3 Comments

What if you add a Class2 object to list2? It will give a ClassCastException in this line System.out.println(((Class1)list2.get(0)).getA());
actually in second code you need to cast the object of type Serializable to Class1.. this is just an example...
The correct solution to this is as others have already pointed out - create a new interface that defines the contract for getA(). This is just arbitrarily casting an element, which you have no guarantee is compatible with what you're casting it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.