A note on abstract methods

In developing the GClient (detailed in the preceeding post), I discovered for myself that the method to be implemented for some interfaces and object types (such as run() in a Runnable object) are done by declaring them as ‘abstract’ methods. This means when a programmer creates a new instance of that object, the abstract method that needs to be implemented is shown to be ‘filled up’ with context specific code. Below is an example:

This is an abstract method from my ‘PythonConnection’ class:


/**
  * Implement this to act on data received
  * @param data String containing the data received from the remote python host
  */
	public abstract void onReceive(String data);

When a new PythonConnection object is created, the opportunity to implement onReceive() is shown as below:


PythonConnection pyCon = new PythonConnection() {

			@Override
			public void onReceive(String data) {
				//Put code to use the 'data' String here!
				//Update UI, send a response etc...

			}
		};

Where does the ‘data’ String come from, you ask? Well when the underlying Thread receives data from the BufferedReader, it calls onReceive(), supplying the String read from the Reader and continues listening once onReceive() returns. Here is where that occurs in a typical server (such as in the GClient):


/**
  * Start the receiving thread that will call onReceive() when it has data
  */
	private void startListening() {

		receiveThread = new Thread(new Runnable() {

			@Override
			public void run() {
				listening = true;
				System.out.println("Ready to receive.");

				while(listening) {
					try {
						String packet = fromPython.readLine();
						if(packet != null) {
							onReceive(packet);
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});

		receiveThread.start();
	}

Previously to get around this ‘sending data across classes’ problem (which got especially nasty when Threads came into it) I would either pass the object creating the abstract object or use some sort of ‘shared static class’, which wasn’t ideal and generated problems of its own.

This is an example of one of the joys of exploring a language by using it! I foresee much use for this functionality.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: