I have a problem I've replicated in a simple example. I have a parent class structured something like this, in which I return itself in the getMySuperClass method:-
Code:
public class MySuperClass 
{
	public String super_class = "This is the super class";
	
	public MySuperClass getMySuperClass()
	{
		return this;
	}
}
Then I have another class which is a child to this parent class as so:-
Code:
public class MyClass extends MySuperClass 
{
	public String my_class = "This is the sub-class";
}
I then try to retrieve the super class from the child class and return only the parent class in JSON as below:-
Code:
import com.google.gson.Gson;

public class TestClass {

	public static void main(String[] args)
	{
		MyClass myClass = new MyClass();
		MySuperClass mySuperClass = myClass.getMySuperClass();
		Gson gson = new Gson();
		String json = gson.toJson(mySuperClass);
		System.out.println(json);
	}
}
However in the results I'm getting both strings from the Child and Parent class, even though I'm exporting only the parent class:-
{
"my_class":"This is the sub-class",
"super_class":"This is the super class"
}
Why does this happen? I suspect it's because when using "this" in the parent class, it executes in the context of the child class. If so, how can i get round this problem and really only return the parent class? I'd like to only return JSON for the parent, not the child.