1

I'm new to programming and I just learned inheritance a week ago, and have a question for how to design proper class that extends other class. Code below is the Bank Class that stored all the bank account object into ArrayList, which is why I'm extending the class ArrayList in Bank class.

Question 1: Bank class attribute is ArrayList. So that is why I'm calling super() inside of the constructor. Since attribute can created by calling super() because the Bank class extends ArrayList, I thought that no other private attribute is needed in bank, besides the attribute that I created by calling super(). Is this proper way to do inheritance?

Question 2: Since there is no attribute, I am stuck on serialization through ObjectOuputStream. I want to write ArrayList(attribute that I created by doing super() in constructor), but can't because I don't know how to refer to the ArrayList attribute that I created in super constructor. i tried writeObject(this), but it did't work obviously. How can serialize ArrayList?

Question 3: If this is a right way to implements inheritance of Bank class, how can I load ArrayList from ObjectInputStream? Because there is no attribute, I don't know how to refer to the attribute that I make in super(), so I did such a thing like

this = (ArrayList)ois.readObject()

But it didn't work... How can I load ArrayList using deserialization when there is no attribute?

public class Bank extends ArrayList<Account> implements Serializable{
    //no attribute

    public Bank(){
        super();
    }

    //other methods...

    public void saveToBinary() throws IOException{
        FileOutputStream fos = new FileOutputStream("Bank_Account_Inherit_Binary.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(this);//can't do this
        oos.flush();
        oos.close();
    }

    public void loadFromBinary() throws IOException, ClassNotFoundException{
        FileInputStream fis = new FileInputStream("Bank_Account_Inherit_Binary.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object object = ois.readObject();
        this = (ArrayList<Account>)object;//not working b/c "this" is final variable
    }
}
XYZ
  • 83
  • 11
  • For those of you who are curious how to solve this problem, you can simply cast the object from file to ArrayList, then iterate and add all elements by doing this.add(). – XYZ Mar 12 '21 at 09:38

1 Answers1

1

Regarding question 1, it would be better to say that Bank HAS-A ArrayList, rather than Bank IS-A ArrayList. If you extend ArrayList, you allow outside classes to modify the contents of the ArrayList. Usually, you extend an object when you want to expand upon the capabilities offered by that object. Encapsulation is used when you want to use an object's methods and capabilities to fulfill the tasks that your class performs. You should use encapsulation and maintain a private instance variable that contains the list of accounts:

public class Bank {
    private ArrayList<Account> accounts;

    public Bank() {
        accounts = new ArrayList<Account>();
    }

    other methods...
}

If you want to allow clients to access and modify the list of accounts, you can write a getter and setter for it.

If you have a private instance of the list of accounts, the solution to question 2 is simple, you can now pass the ArrayList into the ObjectOutputStream. If you want to refer to the ArrayList superclass, you can cast the this object to the right type: (ArrayList) this, but this should not be necessary for serialization. I think that the problem you are having is that the Account class is not serializable, make sure that in the class header you say that class Account implements Serializable.

chiragzq
  • 388
  • 1
  • 14
  • Thank you for answering my question. I first use containment to create this Bank class, that is following the Has-A relationship. However, professor told me to implements this using inheritance to get a extra points on this assignment. So I somehow need to figured out the way to implement Bank class using inheritance. – XYZ Apr 07 '19 at 18:55
  • Thanks for the reply. Your implementation of the inheritance is fine, I think that your error is that the contents of the ArrayList must also be Serializable. Make sure that in the Account class you say that it implements Serializable. – chiragzq Apr 07 '19 at 19:15
  • Thank you for replying! I totally forgot about implementing Serializable in my other class that is in ArrayList. However, I still have some question left for loading the object from ObjectInputStream when there is no attribute – XYZ Apr 08 '19 at 17:13
  • You should be able to create an `ObjectInputStream` and call the `readObject()` method and cast the result to type `Bank` – chiragzq Apr 08 '19 at 19:45
  • Yes, I can do until the casting. But the problem is, I have to store the object(ArrayList) that I read from ObjectInputStream to the attribute(ArrayList) that I created in constructor(super()). But I feel like there is no way to refer to that attribute, because I created the attribute in parents constructor. Sorry for the awkward explaining, but thank you for replying. – XYZ Apr 08 '19 at 20:43