-1

EDITED: Looks a little cleaner now, reflects where I currently am and what I'm trying to accomplish, and shows the new issue I'm working on (which has a comment beneath it explaining what I'm getting).

public class Main {
class Terrain
{
    private int length, width;
    Terrain(int l, int w)
    {
        length = l;
        width = w;
    }

    public String getTerrainSize()
    {
        return "Land has dimensions " + length + " X " + width;
    }
}
 public class Mountain extends Terrain{
    private int mounts;
    public Mountain(int num, int x, int y) {
//error above: Implicit super constructor Main.Terrain() is undefined. Must explicitly invoke another constructor
//What exactly does this error mean, and how should I fix it?
        mounts = num;
        length = x;
        width = y;
    }
    public String getMountainSize()
    {
        return "Mountains have the dimensions " + length + " X " + width + " with a total of " + mounts + " mountains";
    }
}
  public static void main(String[] args) {
    Terrain T1 = new Terrain(400, 200);
    T1.getTerrainSize();
    Mountain M1 = new Mountain(350, 150);
//error here: The constructor Main.Mountain(int, int) is undefined
//I have a feeling it'll be fixed if I fix the first error, but if not, let me know.
    M1.getMountainSize();
}
}

Sorry if the post is getting a bit long, but I want everyone to see the whole picture.

  • 1
    You need a no-argument constructor in Terrain, or you need to explicitly call the constructor that Terrain does have. (Also it is generally bad practice to declare fields like length and width as `public`). – markspace Apr 18 '22 at 15:12
  • Ill look into what a no-argument constructor is, thank you. I thought that naming it public was bad practice, but it was just something I had did as an attempt to fix the problem. – Pendragonz Apr 18 '22 at 15:15
  • A rule says: If you create your own constructor, - mostly with parameters- you also have to add a constructor without any parameters. – Reporter Apr 18 '22 at 15:16
  • C.f. "Sub-class constructors": https://docs.oracle.com/javase/tutorial/java/IandI/super.html – markspace Apr 18 '22 at 15:16
  • 2
    @Reporter That's a bad rule. – shmosel Apr 18 '22 at 15:16
  • Might be duplicate: https://stackoverflow.com/questions/2319817/how-to-inherit-constructor-from-super-class-to-sub-class – markspace Apr 18 '22 at 15:18
  • Another duplicate: https://stackoverflow.com/questions/8055560/using-parent-constructor-in-a-child-class-in-java – markspace Apr 18 '22 at 15:19
  • I might have fixed the main issue, but now Im getting this error on the Mountain constructor: Implicit super constructor Main.Terrain() is undefined. Must explicitly invoke another constructor – Pendragonz Apr 18 '22 at 15:27
  • I edited the post so it would be cleaned up and up to date. Error I currently have is highlighted by a comment. – Pendragonz Apr 18 '22 at 15:37
  • Does this answer your question? [implicit super constructor Person() is undefined. Must explicitly invoke another constructor?](https://stackoverflow.com/questions/23395513/implicit-super-constructor-person-is-undefined-must-explicitly-invoke-another) – Petter Friberg Apr 23 '22 at 05:28

3 Answers3

0

FINALLY:

public class Main {
static class Terrain
{
    public static int length, width;
    Terrain(int l, int w)
    {
        length = l;
        width = w;
    }

    public String getTerrainSize()
    {
        return "Land has dimensions " + length + " X " + width;
    }
}
 public static class Mountain extends Terrain{
    private int mounts;
    public Mountain(int num, int x, int y) {
      super(length, width);
        mounts = num;
        length = x;
        width = y;
    }
    public String getMountainSize()
    {
        return "Mountains have the dimensions " + length + " X " + width + " with a total of " + mounts + " mountains";
    }
 }
  public static void main(String[] args) {
    Terrain T1 = new Terrain(400, 200);
    System.out.println(T1.getTerrainSize());
    Mountain M1 = new Mountain(8, 350, 150);
    System.out.println(M1.getMountainSize());
}
}
  • This is still incorrect. The `Mountain` constructor should not be assigning the `length` and `width` fields. That is the responsibility of the `Terrain` constructor. That's why you call the `super` constructor, to let it assign them, and it is doing that. When you then assign them again in the `Mountain` constructor that is redundant, and, frankly, shows that you don't understand how it works. – David Conrad Apr 18 '22 at 16:37
  • So what, it should be this?: public Mountain(int num) {super(length, width); mounts = num;} – Pendragonz Apr 18 '22 at 17:17
  • Yes, that's right. – David Conrad Apr 18 '22 at 19:32
0

To clarify, you need to know the byte-code level in order to know how things work under the hood.

Let's get started with this piece of code:

public class ParentConstructor {
    public ParentConstructor() {
    }
}

Then run compile (javac - java compile) and extract information (javap - java print) command:

$~ javac /path/to/ParentConstructor.java
$~ javap /path/to/ParentConstructor.class

You will get:

{
  public ParentConstructor();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 2: 0
        line 3: 4
}

The first instruction pushes this on the operand stack. The second instruction pops this value from the stack and calls the <init> method defined in the Object class. This corresponds to the super() call, i.e. a call to the constructor of the superclass, Object.

ParentConstructor can be rewritten as follow:

public class ParentConstructor {
    public ParentConstructor() {
       super();
    }
}

It means you can implicitly call the superclass's constructor in your subclass constructor if the superclass has the default constructor or explicitly call the superclass's constructor in your subclass constructor if the superclass doesn't have the default constructor.

public static class Terrain
    {
        public int length, width;
        Terrain(int l, int w)
        {
            length = l;
            width = w;
        }
        
        public String getTerrainSize()
        {
            return "Land has dimensions " + length + " X " + width;
        }
    }
    public static class Mountain extends Terrain{
        private int mounts;
        public Mountain(int num, int x, int y) {
            //explicitly call superclass's constructor.
            super(x, y);

            //Error above, I'm not sure why this wont work when it works just fine in the parent class.
            mounts = num;
            length = x;
            width = y;
        }
        public String getMountainSize()
        {
            return "Mountains have the dimensions " + length + " X " + width + " with a total of " + mounts + " mountains";
        }
    }

or add a default constructor to the superclass.

public static class Terrain
    {
        public int length, width;
        Terrain(int l, int w)
        {
            length = l;
            width = w;
        }
        
        public Terrain() {
        }
        
        public String getTerrainSize()
        {
            return "Land has dimensions " + length + " X " + width;
        }
    }

Reference:

logbasex
  • 1,688
  • 1
  • 16
  • 22
-1

Try This solution + Explanation

This error is also new to me. Terran class is a parent class and the Mountain class is extended one. So those classes are using constructors.

When you make object from parent class, you can do it on this way:

Terran t1 = new Terran(400, 200)

If you are trying to make object from Mountain(sub class)class. You must pass parameters to sub class constructor and your parent class constructor.

so you can code it like this :

class Mountain{
   private mounts;
   Mountain(int num, int l, int w) {
        super(l,w);
        this.mounts = num;
    }
}

Super() method pass parameters to paraent class constructor If my explanation have any fault. let meknow in comment

Try This code:

class Terrain{
 private int length=0, width=0;
    
Terrain(int l, int w)
    {
        this.length = l;
        this.width = w;
    }
    public String getTerrainSize()
    {
        return "Land has dimensions " + this.length + " X " + this.width;
    }
}

class Mountain extends Terrain{
    
   private int mounts=0,length=0,width=0;
     
    Mountain(int num, int l, int w) {
        super(l,w);
        this.mounts = num;
        this.length=l;
        this.width=w;
        
    }
    
    public String getMountainSize()
    {
        return "Mountains have the dimensions " + this.length + " X " + this.width + " with a total of " + this.mounts + " mountains";
    }
     
 }
class Main{
    public static void main(String[] args) {
    Terrain T1 = new Terrain(400, 200);
    System.out.println(T1.getTerrainSize());
    Mountain M1 = new Mountain(8, 350, 150);
    System.out.println(M1.getMountainSize()); 
} 
}
ToshaEX
  • 1
  • 2
  • This website was created in large part to supplant w3school because it was so bad. I don't know if it has gotten any better in years since, but I would not recommend that anyone go to it. – David Conrad Apr 18 '22 at 16:38
  • 1
    Your code does not compile. It has the same flaw OP's code has. – David Conrad Apr 18 '22 at 16:39
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 18 '22 at 19:45
  • @DavidConrad You are absolutely correct, I'm currently an Undergraduate. I am used W3School to learn the basics. Advaced areas like this never coverd by W3school. Can you Provide some suggestions to proper ways to cover advanced subject areas – ToshaEX Apr 23 '22 at 05:21
  • These aren't really advanced areas. Objects, fields, and constructors are the basics. You have fields in your subclass that are shadowing the ones in the parent class. I would suggest reviewing the [Java Tutorial on subclasses](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) and the other [Java Tutorials](https://docs.oracle.com/javase/tutorial/java/TOC.html) on the Oracle website. – David Conrad Apr 23 '22 at 05:47