Extend a java class from one file in another java file

325,875

Solution 1

Just put the two files in the same directory. Here's an example:

Person.java

public class Person {
  public String name;

  public Person(String name) {
    this.name = name;
  }

  public String toString() {
    return name;
  }
}

Student.java

public class Student extends Person {
  public String somethingnew;

  public Student(String name) {
    super(name);
    somethingnew = "surprise!";
  }

  public String toString() {
    return super.toString() + "\t" + somethingnew;
  }

  public static void main(String[] args) {
    Person you = new Person("foo");
    Student me = new Student("boo");

    System.out.println("Your name is " + you);
    System.out.println("My name is " + me);
  }
}

Running Student (since it has the main function) yields us the desired outcome:

Your name is foo
My name is boo  surprise!

Solution 2

What's missing from all the explanations is the fact that Java has a strict rule of class name = file name. Meaning if you have a class "Person", is must be in a file named "Person.java". Therefore, if one class tries to access "Person" the filename is not necessary, because it has got to be "Person.java".

Coming for C/C++, I have exact same issue. The answer is to create a new class (in a new file matching class name) and create a public string. This will be your "header" file. Then use that in your main file by using "extends" keyword.

Here is your answer:

  1. Create a file called Include.java. In this file, add this:

    public class Include {
        public static String MyLongString= "abcdef";
    }
    
  2. Create another file, say, User.java. In this file, put:

    import java.io.*;
    
    public class User extends Include {
        System.out.println(Include.MyLongString);
    }
    

Solution 3

Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them. The closes thing to an include statement java has is 'import'. Since classes are broken up into namespaces like foo.bar.Baz, if you're in the qux package and you want to use the Baz class without having to use its full name of foo.bar.Baz, then you need to use an import statement at the beginning of your java file like so: import foo.bar.Baz

Solution 4

You don't.

If you want to extend Person with Student, just do:

public class Student extends Person
{
}

And make sure, when you compile both classes, one can find the other one.

What IDE are you using?

Share:
325,875
Jin Yong
Author by

Jin Yong

Updated on February 10, 2020

Comments

  • Jin Yong
    Jin Yong about 4 years

    How can I include one java file into another java file?

    For example: If I have 2 java file one is called Person.java and one is called Student.java. How can I include Person.java into Student.java so that I can extend the class from Person.java in Student.java

  • Jin Yong
    Jin Yong almost 15 years
    Before I do extends for my public class, do not me have to including the file for Person into Studnet 1st? Do you know the statement for include file in java? For example in php the statment is include('directoty/file_name');
  • Pablo Santa Cruz
    Pablo Santa Cruz almost 15 years
    Hi Jin. No, you don't need to "include" the file for Person. You just extend the class. If classes are not in the same package, you "import" the class. But there is no need to include the file. In fact, you can't do that. Even if you want to. Are you using some kind of IDE? Eclipse? Netbeans?
  • Brett
    Brett almost 15 years
    It's automatic if the classes are in the same package (including default packages for source files that don't have an explicit package).
  • Matt G
    Matt G almost 15 years
    No. Java will look for the class in your [Classpath][1]. If it's in the same directory, and you're not using packages, then it'll find it. [1]: en.wikipedia.org/wiki/Classpath_%28Java%29
  • Andrew Farrell
    Andrew Farrell over 8 years
    Don't you need some sort of import statement at the top of Student.java? What does that import statement look like?
  • ApproachingDarknessFish
    ApproachingDarknessFish over 8 years
    @AndrewM.Farrell No, an import statement is not necessary. Simply place the files in the same directory and compile them. They will be able to "see" each other just fine.