2

Say I have the following two classes:

package MyShape;

public abstract class Shapeclass {
    public abstract double area(double radius);

    protected void print(double area) {
        System.out.print("Area is:" + area);
    }
}

And another class, Triangle;

package MyShape;

public class Triangle extends Shapeclass {
    double area;

    public double area(double radius) {
        return radius * radius;
    }

    public void print() {
        super.print(area);
    }
}

I have put both classes in the same folder named MyShape. But when I try to compile the Triangle class compiler, it shows the following error;

C:\Users\Desktop\MyShape\Triangle.java:3: error: cannot find symbol
public class Triangle extends Shapeclass
                              ^
  symbol: class Shapeclass
C:\Users\Desktop\MyShape\Triangle.java:13: error: cannot find symbol
super.print(area);
^
  symbol:   variable super
  location: class Triangle

How can I solve it?

5
  • 1
    Unless you mean relative names, you are not doing what you say you are doing. Commented Nov 24, 2012 at 19:23
  • 1
    You need to compile shapeclass before Triangle class Commented Nov 24, 2012 at 19:38
  • What is the exact command you are using to compile Triangle.java? Commented Jan 19, 2023 at 20:26
  • Why was "separate folder" changed to "same folder" in revision 3? A guess (the OP is gone - "Last seen more than 8 years ago")? Was it due to Mukul Goel's answer? Related: Exit strategies for "chameleon questions" Commented Jan 19, 2023 at 20:40
  • My bad...I didn't realize this is a very old question... Commented Jan 19, 2023 at 21:00

6 Answers 6

2

As long as they're in the same folder and package, you'll be OK.

However, I would make the following changes to your classes:

  1. Rename Shapeclass to Shape... we know it's a class already. Putting "class" in a class name adds no value
  2. Since you require subclasses to declare an area() method, call that from within your print() method
  3. Don't store the area value in your shape - just call thearea()` method if you need it

Making these changes simplifies your code a lot:

public abstract class Shape {
    public abstract double area(double radius);

    protected void print() { // don't pass anything in!
        System.out.print("Area is:" + area()); // call the area() method
    }
}

public class Triangle extends Shape {
    double radius; // triangles don't have radii, you'll need to store something else

    public double area() {
        return radius * radius; // This is a questionable calculation. fix it
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

many thanks But right now just conerend about how to compile Triangle class that is not working.
1
  1. Verify the filenames match the class names, exactly - upper/lower case on each letter.

  2. Verify that your current working directory (CWD) is at the root of your package hierarchy; according to your compiler output that would be "C:\Users\Desktop". This is so that the compiler can find the package path "MyShape" as an immediate subdirectory.

  3. Specify the fully qualified package\class name (MyShape\Triangle) as the filename to compile.

Thus your command-line, showing CWD as the prompt and the javac command, might look like (on Windows):

cd C:\Users\Desktop
javac MyShape\Triangle

Comments

0

From C:\Users\Desktop ...

javac -cp . MyShape\Triangle.java

OR if you aren't in C:\Users\Desktop ...

javac -cp C:\Users\Desktop C:\Users\Desktop\MyShape\Triangle.java

Comments

0

You have mentioned

I have put both classes in separate folder named MyShape

As per JLS, it is required to have both of the classes in the same folder.

2 Comments

It goes in the right direction, but the rule is, that both classfiles are expected in folder MyShape because both classes are defined for the package MyShape. (This still isn't a strict rule, but correct for almost all use cases)
Re "...separate folder": That was changed to the opposite in revision 3.
0

Program of two classes in a single package

Here is the code of package with two classes:

package pack;

class pack_add_sub
{
    public void ad (int x, int y)
    {
        System.out.println("The Add of  " + x + " & " + y + " is: " + (x + y));
    }
}

public class pack_add_subb extends pack_add_sub
{
    public void su (int x, int y)
    {
        super.ad(x, y);
        System.out.println("The Sub of  " + x + " & " + y + " is: "+  (x - y));
    }
}

Now we compile it and save class file in the package name folder. which created in bin folder.

Now, here is the code to call both classes which created in package:

import java.io.*;
import java.util.*;
import pack.pack_add_subb;

class calling_pack_add_subb
{
     public static void main (String arr[])
     {
         int a, b;
         Scanner ob = new Scanner (System.in);
         System.out.println("Enter the same value for add and sub: ");
         a = ob.nextInt();
         b = ob.nextInt();

         pack_add_subb ob1 = new pack_add_subb ();
         ob1.su(a, b);
     }
}

Comments

0

Both classes, ShapeClass and Triangle, need to be in the same folder if you are using the basic javac.

If you have them in different folders, you would need to use the -d option of javac to specify the directory. Refer to here for details: javac

So it’s advisable to keep both files in the same folder, MyShape.

5 Comments

Both are in the same folder but still geting this error even i compiled the Shape class first and then triangle class.
@Kameron what is the name of folder they.are in..it has to be same as package name
Both classes (Shapeclass.java and Triangle.java) and Shapeclass.class file resides in same folder MyShape which is the name of package MyShape. But don't know why getting the errors?
@kameron Try javac Shapeclass.java when it completes try javac -cp MyShape.Shapeclass Triangle.java let me know if it works.
Tried when run javac -cp MyShape.Shapeclass Triangle.java got the same errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.