2

Take for example the class JSONObject.

And say I have the following method.

public JSONObject getObjectOrThrow(String name) {
      JSONObject jsonObject = null;
      try {
         jsonObject = JSON.getJSONObject(name);
      } catch (Exception e) {
         System.out.println("JSON_ERROR : " + name);
         e.printStackTrace();
      }
      return jsonObject;
   }

How do I extend the JSONObject so that I'll end up with a method like.

JSONObject man = new JSONObject("name");
man.getObjectOrThrow("name");

Where "name" is the key to the child node of "man".

Also for reference, what is this called?

Relm
  • 7,923
  • 18
  • 66
  • 113
  • 2
    How is this related to *extending* a class? You're not even using the child class. – Tom Nov 21 '16 at 05:36
  • @Tom Ok, seems I'm even way off the road then, can you point me in the right direction? – Relm Nov 21 '16 at 05:37
  • Since it is currently unclear (at least to me) what you're trying to achieve, no I can't give any hints right now. – Tom Nov 21 '16 at 05:41
  • 1
    Too bad [Java don't have extension methods](http://stackoverflow.com/questions/4359979/java-equivalent-to-c-sharp-extension-methods) – xiaofeng.li Nov 21 '16 at 05:46
  • @Relm you wanted to hide the exception handling to a custom method by overriding the functionality ? Just by creating another class ? Is that your question ? – Vinay Veluri Nov 21 '16 at 05:48
  • @VinayVeluri in this case that's what should happen, but I wrote this as an example so that the answer is not limited to this. All I want is : `existingKnownObject.myCustomMethod()` – Relm Nov 21 '16 at 05:51

1 Answers1

1

Custom Class

public class CustomJSONObjectProvider extends JSONObject {
    public JSONObject getObjectOrThrow(String name) {
      JSONObject jsonObject = null;
      try {
         jsonObject = JSON.getJSONObject(name);
      } catch (Exception e) {
         System.out.println("JSON_ERROR : " + name);
         e.printStackTrace();
      }
      return jsonObject;
   }

   // all the other custom methods that you want to override

}

And the way you use it

CustomJSONObjectProvider customJSONObjectProvider = new CustomJSONObjectProvider();
JSONObject jsonObject = customJSONObjectProvider.getObjectOrThrow("name");

It can also be static

public class CustomJSONObjectProvider extends JSONObject {
    public static JSONObject getObjectOrThrow(String name) {
      JSONObject jsonObject = null;
      try {
         jsonObject = JSON.getJSONObject(name);
      } catch (Exception e) {
         System.out.println("JSON_ERROR : " + name);
         e.printStackTrace();
      }
      return jsonObject;
   }

   // all the other custom methods that you want to override

}

Its usage

JSONObject jsonObject = CustomJSONObjectProvider.getObjectOrThrow("name");
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • 1
    So then I have to pass **JSON** in `JSON.getJSONObject(name)` to CustomJSONObjectProvider. The reason I wanted to do this was so that I never have to pass the object to this class. Thought the Dot Notation would get which object I'm referring to. – Relm Nov 21 '16 at 06:10
  • @Relm What is object there ? You wanted to extend the method and provide custom code. What do you mean by `Dot notation will get which object you refer to` ? – Vinay Veluri Nov 21 '16 at 06:12