Implement Serializable to a class implementing an Interface

1.1k views Asked by At

I have this super class named Person which is inherited by two other classes, Employee & Client I am using an interface so as I can use generics on the two sub classes and hence the Person class implements this interface, Searchable. Is it possible for the Person class to implement both the interface and Serializable so I can save?

package compuwiz;
public abstract class Person implements Searchable //implements Serializable ??
{
public Person()
{
    pName = "";
    pSurname = "";
    pIdCard = "";
}
public Person(String nm, String sn, String id)
{
    pName = nm;
    pSurname = sn;
    pIdCard = id;
}
String pName;
String pSurname;
String pIdCard;
public String GetName()
{
    return pName;
}
public String GetSurname()
{
    return pSurname;
}

@Override
public String GetID()
{
    return pIdCard;
}
//Set Methods
public void SetName(String nm) 
{
  pName=nm;  
}
public void SetSurname(String sn) 
{
  pSurname=sn;  
} 
public void SetID(String id) 
{
  pIdCard=id;  
}

@Override
public String ToString()
{
    return this.GetName()+ " " +this.GetSurname()+ "ID card number:" +this.GetID();
}
2

There are 2 answers

0
Luiggi Mendoza On BEST ANSWER

Yes. That's the purpose of interfaces. A class can implement multiple interfaces at the same time:

public abstract class Person implements Searchable, Serializable {
    //current code of your class...
}

Besides this, I would recommend using proper naming of your methods. This is, use camelCase standard, the first letter goes in lower case, then you use upper case for the next word contained in the name. Example:

//this will throw a compiler error unless you define a ToString method in a top interface
@Override
public String ToString() {
}

To

@Override
public String toString() {
}
0
Mateo On
public abstract class Person implements Serializable, Searchable {}

Java suppports multiple interface inheritance