Home > Mobile >  How Do You Make an Object From an Inheritance Class in Java?
How Do You Make an Object From an Inheritance Class in Java?

Time:04-19

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.

CodePudding user response:

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

CodePudding user response:

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:

CodePudding user response:

Try to learn Java Constructors from w3School.

if you access length or width values within the method. you must put this keyword before the length or width.

Code :

public class Terrain
{
 private int length, width;
    public Terrain(int l, int w)
    {
        length = l;
        width = w;
    }
    public String getTerrainSize()
    {
        return "Land has dimensions "   this.length   " X "   this.width;
    }
}
public class Mountain extends Terrain{
    private int mounts,length,width;
    public Mountain(int num, int x, int y) {
        mounts = num;
        length = x;
        width = y;
    }
    public String getMountainSize()
    {
        return "Mountains have the dimensions "   this.length   " X "   this.width   " with a total of "   this.mounts   " mountains";
    }
  }```
  • Related