-1

My first class.(named Person.java)

public class Person {
 // Instance variables
 private String name;
 private String address;
 // Constructor
 public Person(String name, String address) {
 this.name = name;
 this.address = address;
 }
 // Getters
 public String getName() {
 return this.name;
 }
 public String getAddress() {
 return this.address;
 }
 public String toString() {
 return this.name + "(" + this.address + ")";
 }
}

My second class.(named Student.java)

public class Student extends Person {
// Instance variables
private String subject;
// Constructor
public Student(String name, String adress, String subject){
    super(name, adress);
    this.subject = subject  
}
// getters
public String getSubject(){
    return this.subject ;
}

}

My class that uses those two classes.(run method is like the main)

import acm.program.*;

public class Test extends Program{

   public void run() {
      Student obj1 = new Student("Kwnstantinos", "Liakataiwn", "math");
}
}

I am writing the following in the cmd to compile it but I got cannot find symbol error in compilation.

javac -cp acm.jar Person.java Student.java Test.java

I tried to make an Person's class object and it worked but when I am trying to create a Student's class object I get this error.Why is this happening.

C:\Users\Kwnstantinos\Desktop\JAVA>javac -cp acm.jar Person.java Student.java Test.java
Test.java:6: error: cannot find symbol
      Student obj1 = new Student("Kwnstantinos", "Liakataiwn", "math");
      ^
  symbol:   class Student
  location: class Test
Test.java:6: error: cannot find symbol
      Student obj1 = new Student("Kwnstantinos", "Liakataiwn", "math");
                         ^
  symbol:   class Student
  location: class Test
2 errors
Kwnstantinos Nikoloutsos
  • 1,832
  • 4
  • 18
  • 34

1 Answers1

0

It looks like you have not imported the Student class in the Test class, and these two classes Test.java and Student.java are in different packages.

This line needs to have a semicolon at the end

this.subject = subject;

After that its compiling fine without the jar import.

Yes the other answer was correct you need to have it like this

javac -cp . Person.java Student.java Test.java if you remove the acm from the import

and they are compiling fine.

muasif80
  • 5,586
  • 4
  • 32
  • 45