Here I have a constructor
in super
class
and corresponding constructor
in child
class
.
As far as I know in such cases in child
constructor
I need to use the super
keyword.
I have written my program as such -
public class inheritance_demo6 {
public static void main(String[] args) {
Dog dog = new Dog("Rohu");
System.out.println("My name is: " + dog.getName());
dog.eat();
}
}
class Animal {
protected String name;
Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println("I am eating...");
}
public String getName() {
return name;
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
}
However on execution I am getting compile time error -
Exception in thread "main" java.lang.NoSuchMethodError: 'void Dog.<init>(java.lang.String)'
at inheritance_demo6.main(inheritance_demo6.java:3)
It says the error is in line:3. In line:3 I am creating an instance of the class
Dog
. So if I understand correctly the error is due to some incorrect code while writing the child
class
constructor
.
But I am unable to determine where the error can be.
I am using Visual Studio Code editor on Ubuntu.
UPDATE:
If I run the code on terminal independently it runs perfectly. It gives error only on VSCode
Here is my screenshot on VSCode -
My Java Version -
$ java --version
openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.21.10)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.21.10, mixed mode, sharing)
Here I tried running a simple program "Hello World" in VSCode to check if there is any error in VSCode installation or some settings maybe. But it runs perfectly . Here is the screenshot -
UPDATE:
I moved the three classes to three different files -
InheritanceDemo6.java
-
public class InheritanceDemo6 {
public static void main(String[] args) {
Dog dog = new Dog("Rohu");
System.out.println("My name is: " + dog.getName());
dog.eat();
}
}
Animal.java
-
public class Animal {
protected String name;
Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println("I am eating...");
}
public String getName() {
return name;
}
}
Dog.java
-
public class Dog extends Animal {
Dog(String name) {
super(name);
}
}
Here, getting some strange errors -
In class Dog.java
getting the error -
Animal cannot be resolved to a typeJava(16777218)
In class InheritanceDemo6.java
getting the error -
Dog cannot be resolved to a typeJava(16777218)
On doing a ls
on terminal, all the three java files are in the same directory.
Interestingly here compilation fails even when doing it independently in terminal. Here is the error -
payel@payel-Lenovo-ideapad-330-15IKB:~/VisualStudioCode/John_Purcell_Java_Basics$ java InheritanceDemo6.java
InheritanceDemo6.java:3: error: cannot find symbol
Dog dog = new Dog("Rohu");
^
symbol: class Dog
location: class InheritanceDemo6
InheritanceDemo6.java:3: error: cannot find symbol
Dog dog = new Dog("Rohu");
^
symbol: class Dog
location: class InheritanceDemo6
2 errors
error: compilation failed
Here, the error in VSCode is -
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Dog(String) is undefined
The method getName() is undefined for the type Dog
at InheritanceDemo6.main(InheritanceDemo6.java:5)
The problem lies within that the application entry point(Class containing main() method) resides at the VS-Code project/root level, which will not necessarily points VS-code to the correct Class-path. Open/expand VS-code explorer and look in the JAVA PROJECTS view, to see which classes are contained within the class-path.
A good way to start a new project is to use VS-code integrated project manager: open VS-code > ctrl+shift+p > "create java project" > choose an option with or without management tools (no build tools, maven, gradle, etc)
ps: using no build tools will still use VS-Code to debug, compile & run your code.