1

Consider I have a class like below.

public class SuperClass
{
  private String a;
  private String b;
  //getter/setters
}

And a subclass

public class ChildClass extends SuperClass
    {
      private String c;
      private String d;
      //getter/setters
    }

In another class I have an object of the ChildClass. This object has both the parent and child class member variables populated with data.
Using this object, I need to get an instance of parent class, i.e, an object which only has the variables in the parent class (i.e., a and b). How can I achieve it ?

jijo
  • 765
  • 3
  • 18
  • 35
  • 1
    Most of the time it is sufficient for treating object of ChildClass as SuperClass. What is the background that you should not include `c` and `d` in the object ? – ymonad Feb 15 '19 at 00:29
  • 2
    This sounds like an XY problem. Every instance of `ChildClass` *is* an instance of `SuperClass`. Why do you need to create an instance of `SuperClass` that *isn't* an instance of `ChildClass`? – ruakh Feb 15 '19 at 00:30
  • I'm converting this object to json and it shouldn't be having the child class fields. Can't use @JsonIgnore because the child class fields are needed in json for some other scenario. – jijo Feb 15 '19 at 01:13
  • Have you looked at @JsonFilter with SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames) for run-time decisions of what to serialize ? eg https://proliferay.com/create-json-by-jackson-api/ – racraman Feb 15 '19 at 01:35

2 Answers2

3

Let's take a step back and understand what happens when you create an instance of the ChildClass. A memory location is made to store the fields of ChildClass and SuperClass. Now you cannot make that memory location "forget" the ChildClass fields and retain only the SuperClass fields. The best you can do is make the ChildClass fields null.

So to do what you want, you'll have a create a new object. You can do so by using a method like this:

public class ChildClass extends SuperClass {
    private String c;
    private String d;

    public SuperClass getSuperClassInstance() {
        SuperClass sc = new SuperClass();
        sc.a = this.a; //or use getters/setters
        sc.b = this.b; //you might need to deep clone objects
        return sc;
    }
}

As @ruakh said, merely casting to SuperClass is not enough as the data can be retrieved by casting it back to ChildClass as long as it is pointing to the same memory location.

Update based on comments

So this is an XY problem!

You can annotate the fields in your child class by @JsonIgnore or do it at class level using @JsonIgnoreProperties(value = { "c", "d" }).

Also, the answer in this duplicate question should help.

If you need to serialize those fields in some other case, you can use JSON Views.

Kartik
  • 7,677
  • 4
  • 28
  • 50
2

You can achieve this by casting the class, if you have an ChildClass instance, you can get the SuperClass with the following command:

SuperClass sc = (SuperClass) childClass;

or

SuperClass sc = childClass;

As the comments bellow say, you don't really need the cast in this case, just making a SuperClass variable receiving your ChildClass is enough.

This happens because the Upcast (from child to super) is always possible, so it's safe to do it implicit. Only needing to explicit in cases of Downcast.

Here you can learn more about java casting.

Fernandoms
  • 444
  • 3
  • 13
  • 2
    As you probably realize, this doesn't actually create a new instance; `sc` and `childClass` will refer to the same object, with the same fields, and `((ChildClass)sc)` will still work the same as `childClass`. – ruakh Feb 15 '19 at 00:33
  • 2
    You don't need to cast - you can just say `SuperClass sc = childClass;`. Casting doesn't change anything about the instance. – Erwin Bolwidt Feb 15 '19 at 00:34
  • As @ruakh pointed out 'sc' has the fields of child class as well. I need an object which has the parent class fields alone. I'm converting this object to json and it shouldn't be having the child class fields. – jijo Feb 15 '19 at 01:08