0

The usage of the import statement is pretty trivial. It essentially allows us to "bring in" the functionality of classes not in our package.

For example, consider the classes below:

package wolfPack;

public class Wolf {
    //Scary Code Here
}
package sheepHerd;

public class Sheep {
    //Fluffy Code Here
}

We can import these classes into another package pretty easily.

import wolfPack.Wolf;
import SheepHerd.Sheep;

package jungle;

public class Jungle {
    public static void main(String[] args) {
        Sheep sheep1 = new Sheep();
        Wolf wolf = new Wolf();
        //TODO: fight code
    }
}

Now, imagine that we had some non-public classes in the public class files above. Say, we wanted to create some smart Sheep that could tell when wolves were nearby. In other words, we add a SmartSheep class in our Sheep.java file.

package sheepHerd;

public class Sheep {
    //Fluffy Code Here
}

class SmartSheep extends Sheep {
    //Smart Code here
}

Now, how exactly would we go about importing the SmartSheep into our Jungle class? Will it be imported by simply typing import sheepHeard.Sheep?

Dr. Java
  • 63
  • 6
  • 1
    Does this answer your question? [What is the difference between public, protected, package-private and private in Java?](https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in) – Deadbeef Nov 13 '20 at 07:32
  • 3
    You don't. Only public classes can (and should) be used from other packages. that is the whole point of making things public. Beyond that, please understand: this community isnt a replacement for the "learning" part. What you ask is a very basic topic - that is covered in any good book or tutorial about Java. Always remember: when you are learning new things, all the questions you can think of ... have probably been asked here before. And explained here, and in many other places. So please do a bit more research before asking something that a book/search engine could *easily* tell you in 5 min – GhostCat Nov 13 '20 at 07:35
  • 2
    And unrelated: you dont use camel case for package names, see https://stackoverflow.com/questions/3179216/what-is-the-convention-for-word-separator-in-java-package-names for example. – GhostCat Nov 13 '20 at 07:36
  • @GhostCat Oh, I do apologize! Could you please reference the tutorial that you speak of? I did search for about 15 minutes, as I'm by nature a lazy person who'd rather not type out a detailed post, but I wasn't able to find anything that answered my specific question. – Dr. Java Nov 13 '20 at 07:39
  • @GhostCat Allow me to apologize once again, as you can see I'm not the sharpest tool in the shed! I realize what you mean now, I didn't think about that at all when asking. My bad for cluttering your feed. – Dr. Java Nov 13 '20 at 07:44
  • 1
    No worries. You spent your time to write up a proper question, I took my time to respond, and you listened and will do better the next time. A good day for all of us. But you can always go in and delete comments that arent necessary any more for example ;-) – GhostCat Nov 13 '20 at 07:57

2 Answers2

1

A class that is not public will not be importet. If you want to use it from another package make it public and move it to its own file.

Henry
  • 40,427
  • 6
  • 56
  • 72
1

The class SmartSheep is package-private, which means it is only visible to members of the same package. You must declare the animals in the same package as the jungle, which makes sense that way, because these animals all belong to the jungle. Move Wolf and Sheep to package jungle instead.

Deadbeef
  • 887
  • 3
  • 14