I’m making a messaging service in java where I’m using sockets and object input/output streams. I wanted to make this as object oriented as I can (to learn OOP) so for the client side I’m making an abstract client class with three subclasses (owner, administrator and user). The main idea is to put the shared code in the three subclasses in the parent class instead to minimize repeated code.
My question is: If I instantiate a socket and the object output and input streams in the parent’s constructor will it work the same as if I just instantiate them in the subclasses?
To clarify the subclasses Owner, Administrator and user will all have the following code:
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
protected subclassExample(){
this.socket = new Socket("localhost",8000)
this.out = new ObjectOutputStream(socket.getOutputStream());
this.in = new ObjectInputStream(socket.getInputStream());
}
So I wanted to move the above code from the subclasses and into their parent class, then use super in the subclass to instantiate the code instead and use getters to access the socket etc later in the implementation.
Cyberdrum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3