Search code examples
javainheritanceconstructorsubclasssuperclass

Understanding relationship between Java Super and Sub class constructors and methods


This may be a bit general and I apologize if it is too much so. It pertains to an assignment for a college class and as such would like to focus on my understanding of the topic as opposed to what the answer should be. As I understand the situation: I have one parent class called pet, and two subclasses called Dog and Cat. The UML class Diagrams specify that Pet contains the methods createPet(), updatePet(), and petCheckIn(). petCheckIn() requires the ability to create an object if it isn't already, and update one if it is.

The two subclasses contain default constructors, as well as setters and getters for their attributes, but nothing else specific to them.

I don't believe there would be an instance when a Pet object would be needed as the pets being checked in would always be dogs or cats. If constructors are not inherited by the subclasses but methods are, and createPet() is not strictly speaking a constructor, can an inherited method perform the constructor function if designed properly? I feel the implementation should be something like (not concerned about exact syntax as much as I am my understanding of what can and cant be done here):

My pseudocode :

Execute Dog/Cat inherited petCheckIn()
   IF new pet THEN
      Execute Dog/Cat inherited createPet()

   ELSE
      Execute Dog/Cat inherited updatePet()

This is an entry level programming class, and so I cant imagine that the implementation would be terribly complex. Likely I am misunderstanding something/overcomplicating.

Regardless, Thanks for your time.

EDIT: Truly sorry I never got back to you @Karam and @Chung. Life hits like a freight train sometimes lol. Specifying another class was out of scope for the assignment (though most of what I wanted to know seems to have been as well), nothing was specified about the signature (thats methodName + its arguments correct?). Ultimately I believe a lot depends on how the classes would be implemented by the overall program which was not covered in detail. I can pretty confidently say I was just asking questions that were beyond what was intended at that stage of the lesson plan. I wound up using a parametered constructor in the super class, and another in the subclass which passed arguments for superclass fields to the superclass constructor, then initialized the subclass specific fields. Karam, I never wound up actually defining the methods by which the constructors were called, and wasnt able to educate myself on design patterns, but a relative of mine also mentioned an abstract factory. Id say that is likely the best way to handle it. Unfortunately I do not have more time to dig into this right now so Im calling it good.

Cant tell yall how much I appreciate your willingness to help.

Dog.java constructor:

    //Overloaded Constructor that passes parameters to Superclass 
    constructor.
    //TODO: Reconsider which fields need a parameter and which can be set to 
    default at this point in the program. 
    //TODO: Verify that not passing an argument for catSpace will default to 
    -1.
    //FIXME: Is dogSpaceNBR known at this point in program?
    public Dog(int dogSpaceNBRInput, double dogWeightInput, boolean 
    groomingInput, String petTypeInput, String petNameInput, int petAgeInput, 
    int dogSpaceInput, int catSpaceInput, int daysStayInput, double 
    amountDueInput) {  

    // Passes arguments common to all pet subclasses to superclass 
    constructor.
    super(petTypeInput, petNameInput, petAgeInput, dogSpaceInput, 
    catSpaceInput, daysStayInput, amountDueInput); 

    //Dog specific variables
    dogSpaceNbr = dogSpaceNBRInput; 
    dogWeight = dogWeightInput;
    grooming = groomingInput;
    }

Solution

  • If i understand well your question , you want to be able to create different instances of your pet depending on the subclass using only methods

    There is a Design Pattern called Abstract Factory , it is used to ignore the creation of different methods in a concret way

    Example :

    You have the class Pet ( it should be abstract ) that contains the method createPet() {}

    In your sublcass , you'll have to simply redefine the method createPet() { return new Dog() ; /* or */ return new Cat() ;}

    This helps you whenever you create a new sublcass , to redefine the methods without changing in the original class , and this will goes not only for the method createPet() but for all your methods