3

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 ?

4
  • 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 ? Commented Feb 15, 2019 at 0: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? Commented Feb 15, 2019 at 0: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. Commented Feb 15, 2019 at 1:13
  • Have you looked at @JsonFilter with SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames) for run-time decisions of what to serialize ? eg proliferay.com/create-json-by-jackson-api Commented Feb 15, 2019 at 1:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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.
You don't need to cast - you can just say SuperClass sc = childClass;. Casting doesn't change anything about the instance.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.