FAQ: Inheritance and Polymorphism - Inheriting the Constructor

This community-built FAQ covers the “Inheriting the Constructor” exercise from the lesson “Inheritance and Polymorphism”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Inheriting the Constructor

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Thanks everyone!
So in this “inheriting the Constructor” topic, “Noodle.java” is the parent class and in a different file, “Pho.java” is the child class, are there any rules to link two different files without importing one file in the other? I mean, for example…once the Pho class inherits from Noodle in Pho.java, we go back to Noodle.java and create a new Pho object. I understand they are linked, but is there any rule? Do They have to be in the same directory? or only with the name is enough??

Hey @patodeville, maybe I can help you. I am not on this lesson yet, but usually you have to specify the full file path if the file you are trying to import is not in the same directory.

Hope this will help!

But, if the file is in the same directory…

you can use

import ./<filename>

where filename is the name of the file.
P.S. I may not be correct, as I am not on this lesson yet

Hello everyone,

I am unclear on how and why “this.” is being used in the constructor. For example, “this.shape = shp”.

I don’t recall using “this.” when making constructors in previous lessons. Does this have something to do with inheritance to a child class?

In Java, this keyword is used to refer to the current object inside a method or a constructor. For example,

class Main {
    int instVar;

    Main(int instVar){
        this.instVar = instVar;
        System.out.println("this reference = " + this);
    }

    public static void main(String[] args) {
        Main obj = new Main(8);
        System.out.println("object reference = " + obj);
    }
}

What it is saying is that this represents int instVar.
Java this Keyword

Typo in this lesson:

Let’s take a look at the super() constructor works!

Should read:
Let’s take a look at [how] the super() constructor works!

1 Like

Hi Pato

The classes are part of a larger package called “package”. Classes within the same package will be able to access each other. That is how they are connected.

1 Like

Hi Tony.

Go back to the lessons that cover public, private, scope, encapsulation etc. It’s covered there.

In a nutshell, you know how you access a field inside a class using the “.”? For example, if there was a class called “Car” with a field called “name” you would access it using “Car.name”?

Well, “this” just replaces the object name, when you are coding inside the object itself. For example, if you were inside the class “Car”, and you wanted to access the field “name”, it would be wierd to reference it as “Car.name” since you have not instantiated/created the object. So the code “this” takes place of the object name.

The general rule you can then follow is, whenever you want to access the instance variable, use “this” in front of it.

The lesson goes through this in more detail and does quite a good job imo.

What I don’t understand is, how can I create an instance of Pho ( the child class) inside Noodles (the parent class) Shouldn’t this be the other way around? Since Pho is inheriting everything from Noodles but not the other way around.

Hi everyone! Question about this lesson; for the second task of printing out phoChay’s shape, is it supposed to print “flat”? It seems to run with no error for me, but the task doesn’t show up as completed.

1 Like

This code worked for me.

    Pho phoChay = new Pho();
    System.out.println(phoChay.shape);

//result “flat”

Im having a general problem with these lessons so far (as i see some others are too) in terms of the flow being used. It seems the main and referencing is being done in the parent (Noodle) which is pulling in child objects. I would think since the child inherits characteristics of the parent, the child (Pho) is where main() would be and pulling in methods, var, and constructors from parent.

The flow of these 2 classes seems the be opposite of that. Anyone else?

Also, in the explanation, they said there are 2 ways to access the parent. One is by using the parent constructor inside the child constructor (Triangle and Shapes) using “super(3)” inside the child constructor. This worked and i tested it.

But when i used the alternative way, which was using “this.numSides = 3” inside the child constructor, it didnt work and i got an error about “Implicit super constructor Shape() is undefined. Must explicitly invoke another constructor”.

According to the explanation, when using this. in this way, the super() constructor is secretly called. I did exactly this way and got the error.

Can any advanced folks explain why it didnt work using this? The codeAcademy should note about this error happening. Try it yourself and see. Will get error.

i found out why the sidebar example in this lesson did not work (using this keyword). After much reseach and talking with pro developers, it seems that the constructior in the super class (parent) must have also a constructor with no arguments (like just Shape() which gets passed when “this” is called. So the sidebar example for using “this” does not work as it is. Try it and see.

I spent so much time to figure out why and paying for these lessons. Code Academy, how about raising the QA on the lesson material? These concepts are challenging enough!

1 Like