Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied

Active3 hr before
Viewed126 times

6 Answers


I am converting JSON to Java.,Is there anyway I can make this work. That is have a generic QueryValue class, but have the correct subclass returned depending on the parameters that the JSON supplies.,Sounds like you want to create a custom deserializer if you need to check the values, or an instance creator if you only need to create an instance based on type., Podcast 378: The paranoid style in application development

I have this fragment of code where I do the conversion.

Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<QueryProperty>>() {}.getType();
Collection<QueryProperty> queryProperties = gson.fromJson(query, collectionType);
load more v

Classes in Java exist in a hierarchy. A class in Java can be declared as a subclass of another class using the extends keyword. A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass itself:,A class can extend only one other class. To use the proper terminology, Java allows single inheritance of class implementation. Later in this chapter, we’ll talk about interfaces, which take the place of multiple inheritance as it’s primarily used in other languages.,A subclass can be further subclassed. Normally, subclassing specializes or refines a class by adding variables and methods (you cannot remove or hide variables or methods by subclassing). For example:,Figure 6-4. Casting and selection of methods and variables

    class IntegerCalculator {
       int sum;

    class DecimalCalculator extends IntegerCalculator {
       double sum;
    int s = super.sum;
    DecimalCalculator dc = new DecimalCalculator();
    IntegerCalculator ic = dc;

    int s = ic.sum; // accesses IntegerCalculator sum
load more v

The World class actually has 2 constructors. One doesn’t take any parameters and one takes the world’s width and height.,Parameters allow values to be passed to the constructor to initialize the newly created object’s attributes.,When a constructor takes more than one parameter.,I and III call the correct constructors.

// To create a new object and call a constructor write:
// ClassName variableName = new ClassName(parameters);
World habitat = new World(); // create a new World object
Turtle t = new Turtle(habitat); // create a new Turtle object
load more v

Creating Application Packages and Classes ,Extension represents the "is-a" relationship. When a class extends another class, it's called a subclass of that class. Conversely, the class being extended is called a superclass of that subclass. ,Generally, use application classes where you can extract the common functionality.,There are four distinct application classes in this example:

In the following example, the class Banana extends the class Fruit. Banana is the subclass of Fruit. From Fruit, you can extend to Bananas. However, from Bananas you can't extend to Fruit. Another way to think about this is you can only extend from the general to the specific, not the other way around.

class Fruit
method DoFruit();
property number FruitNum instance;
end - class;

class Banana extends Fruit​ method DoBanana();
property number BananaNum instance;
end - class;
load more v

You can create classes that inherit some of their properties from another class.,You are familiar with the concepts of inheritance, superclass, and subclass.,This location calls the location of the superclass to calculate the result,Let's modify the Order class so that it includes a reference to a Customer object.

public class Part {

   private String identifier;
   private String manufacturer;
   private String description;

   public Part(String identifier, String manufacturer, String description) {
      this.identifier = identifier;
      this.manufacturer = manufacturer;
      this.description = description;

   public String getIdentifier() {
      return identifier;

   public String getDescription() {
      return description;

   public String getManufacturer() {
      return manufacturer;
load more v

In defining this inheritance hierarchy we have already defined certain restrictions, not all of which are desirable: ,Using inheritance extensively in designing a program imposes certain constraints. ,There are various types of inheritance, based on paradigm and specific language.[7] ,Implementation Inheritance Is Evil

Class A(...); // Base class
Class B: public A(...); // B derived from A
Class C: public B(...); // C derived from B
load more v

Other "instantiated-particular" queries related to "Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied"