Hello. I'm working on a class hierarchy that starts with an abstract class called GeometricObject. I want to use this class to create classes of different types of triangles. I want to these classes to inherit the perimeter method from the Triangle class. Here's what I have so far.

abstract class GeometricObject
{
    //signature for perimeter method
    public abstract double perimeter();

public class Triangle extends GeometricObject
{
    //explicit constructor
    public Triangle(double s1, double s2, double s3)
    {
        this.side1=s1;
        this.side2=s2;
        this.side3=s3;
    }//terminates constructor
    //perimeter method
    public double perimeter()
    {
        return side1+side2+side3;
    }//terminates perimeter method
    //local data fields
    private double side1;
    private double side2;
    private double side3;
}//terminates text of class Triangle
public class Isosceles extends Triangle
{
    //constructor
    public Isosceles( double EQside, double otherside)
    {
        super(EQside, otherside);
    }//terminates constructor
    //local data fields
    private double EQside;
    private double otherside;
}//terminates class Isosceles
public class Equilateral extends Isosceles
{
    //contructor
    public Equilateral(double side)
    {
        super(side);
    }//terminates constructor
    //local data fields
    private double side;
}//terminates class Equilateral

}//terminates class GeometricObject

I'm recieving the following error message:

F:\Java\GeometricObject.java:35: cannot reference this before supertype constructor has been called
        super(EQside, otherside);
        ^
F:\Java\GeometricObject.java:46: cannot reference this before supertype constructor has been called
        super(side);
        ^

There is obviously something wrong with the 2 lines starting with super, but I have not been able to figure it out.
Thanks to everyone in advance for any help.

public Triangle(double s1, double s2, double s3)
	{
		this.side1=s1;
		this.side2=s2;
		this.side3=s3;
	}
	

	
	public Isosceles( double EQside, double otherside)
	{
		super(EQside, otherside); //calls triangle constructor 
	}


	public Equilateral(double side)
	{
		super(side); //calls isosceles constructor. 
	}/

Look at all your constructors. You have one triangle constructor, and it takes three parameters. Then in the Isosceles constructor you call super() with two parameters. You made the same mistake in the Equilateral constructor.

import java.util.ArrayList;
import java.util.List;

abstract class GeometricObject
{
	public abstract String perimeter();
}

class Triangle extends GeometricObject
{
	private double side1;
	private double side2;
	private double side3;
	public Triangle(double s1, double s2, double s3)
	{
		this.side1=s1;
		this.side2=s2;
		this.side3=s3;
	}
	public String perimeter()
	{
		return "Triangle's perimeter method";
	}
}
class Isosceles extends Triangle
{
	public Isosceles(double s1, double s2, double s3)
	{
		super(s1, s2, s3);
	}
	public String perimeter()
	{
		return "Isoceles Triangle's perimeter method";
	}
}
class Equilateral extends Isosceles
{

	public Equilateral(double s1, double s2, double s3)
	{
		super(s1, s2, s3);
	}
	public String perimeter()
	{
		return "Equilateral Triangle's perimeter method";
	}

}
public class Test
{
	public static void main(String... args)
	{
		List<Triangle> list = new ArrayList<Triangle>();
		list.add(new Triangle(0, 0, 0));
		list.add(new Isosceles(0, 0, 0));
		list.add(new Equilateral(0, 0, 0));
		for(Triangle t: list)
			System.out.println(t.perimeter());
	}
}

Triangle's perimeter method
Isoceles Triangle's perimeter method
Equilateral Triangle's perimeter method

Hope that helps.

Hope that helps.

It doesn't help because you just threw code at the OP, didn't offer any explanation to what you changed and didn't even show what you fixed. You should point the OP in the right direction instead of thrusting code at them.

Oh, right. So all of the constructor calls need to have the same number of parameters, but the parameters in the constructor definitions don't need to be the same in number, right? Thanks for your help, jasimp.

I have rewritten my program with your suggestions but I am still getting the same error message.

abstract class GeometricObject
{
	//signature for perimeter method
	public abstract double perimeter();
//begin class Triangle
public class Triangle extends GeometricObject
{
	//explicit constructor
	public Triangle(double s1, double s2, double s3)
	{
		this.side1=s1;
		this.side2=s2;
		this.side3=s3;
	}//terminates constructor
	//perimeter method
	public double perimeter()
	{
		return side1+side2+side3;
	}//terminates perimeter method
	//local data fields
	private double side1;
	private double side2;
	private double side3;
}//terminates text of class Triangle
//begin class Isosceles
public class Isosceles extends Triangle
{
	//constructor
	public Isosceles( double EQside, double otherside)
	{
		super(EQside, EQside, otherside);
	}//terminates constructor
	//local data fields
	private double EQside;
	private double otherside;
}//terminates class Isosceles
//begin class Equilateral
public class Equilateral extends Isosceles
{
	//constructor
	public Equilateral(double side)
	{
		super(side, side);
	}//terminates constructor
	//local data field
	private double side;
}//terminates class Equilateral

For isosceles I used three parameters in the constructor call to match the Triangle constructor. For equilateral I used two parameters in the constructor call to match the isosceles constructor. I thought this was the right way to do it but I am obviously wrong. Thanks again for any help.

If all that code is one Java file than that could be the problem. I separated each class GeometricShape, Triangle, Isosceles, Equilateral, into it's own file and had no trouble running a driver class creating each type of triangle.

I know this is a beginner question but it is something that I have never had to do before. Is this the correct way to import the other classes?:

import Triangle.java.*;
import Isosceles.java.*;
import Equilateral.java.*;

I tried without the asterisks but recieved the same error message which reads:
F:\Java\GeometricObject.java:7: package Triangle.java does not exist
import Triangle.java.*;
^
F:\Java\GeometricObject.java:8: package Isosceles.java does not exist
import Isosceles.java.*;
^
F:\Java\GeometricObject.java:9: package Equilateral.java does not exist
import Equilateral.java.*;
^
I'm doing this in my GeometricObject file. Thanks again for all your help.

I know this is a beginner question but it is something that I have never had to do before. Is this the correct way to import the other classes?

Why would you do this in your GeometricObject class? Just create a driver class, here's the one I made.

public class Tester {
public static void main(String[] args) {
	Triangle tri = new Triangle(56,56,56);
	System.out.println(tri.perimeter());
	Isosceles iso = new Isosceles(56,56);
	System.out.println(iso.perimeter());

}

}

Creating a driver class just tests code from other classes you have built. Your GeometricObject class is an abstract class and has no need to import the other classes because it never uses them. I'm pretty sure that if you keep your Java files in the same folder, say "/Java Code/Geometric" when you compile them the compiler will find all the classes it needs.

commented: Thanks for walking me through the process. +1

Good idea. Thanks so much for all your great advice.

Now referring to your import statements (Although jasimp has already given you the solution for finding the classes you made with "importing" them).
You import the classes and not the .java files, (unlike including the header files in C or C++).

A simple example is show below, here I am including the Hashtable class present in the java.util package. Also observe the second import statement where using the wildcard "*", I am including all the packages present in the java.awt package.

import java.util.Hashtable;
import java.awt.*;

I was able to compile and run the code successfully by separating all the files and creating an additional driver class. Thanks again for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.