'How Do You Make an Object From an Inheritance Class in Java?

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.



Solution 1:[1]

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());
}
}

Solution 2:[2]

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:

Solution 3:[3]

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()); 
} 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pendragonz
Solution 2 logbasex
Solution 3