answersLogoWhite

0


Best Answer

Well, the process differs whether you are using either an IDE or the command line binaries.

If you are using an IDE, then it is most likely that your IDE comes with extensive functionality for both managing and creating packages. Look under the "Packages" menu, or, when you are using Eclipse, just right click on the package name in the Package Explorer window and a flood of possibilities will come right up.

If you are using the command line binaries, then there are 2 steps to putting your classes in a package. First, you have to put your class or interface in the appropriate folder structure. For example, if you are using the package com.foo.bar.beebop, then you have to put your class in a folder named beebop, which is in a folder named bar, which is in a folder named foo, which is in a folder named com, which is in your class directory. Then, you have to add a package statement, which is only allowed to be at the top of your class file. For example, if you're using the package com.foo.bar.beebop, then put the following statement at the top of your class:

package com.foo.bar.beebop;

Then skip a line, then come your import statements (if you have any), then stick in another line (if you have inport statements), then your class.

Enjoy!

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

By adding the fully qualified package name as the file line in the Class or Interface.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you add a class or an interface to a package in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you add import packages to a java program in netbeans?

package thisPackage; import otherPackage.*; class myClass { }


Purpose of marker interface in java?

Generally, they are used to give additional information about the behaviour of a class.It is just used to "mark" Java classes which support a certain capability . Examples: java.util.RandomAccess java.io.Serializable java.rmi.Remote java.util.EventListner javax.servlet.SingleThreadModel java.lang.Clonable javax.ejb.EnterpriseBean


Why do you need runnable interface?

A Runnable Interface is one that is used to create a Java Thread... A Thread can be created in two ways and using the Runnable Interface is one of them. Example: public class Test implements Runnable { public void run(){ .... } } The Runnable interface would have an abstract instance of the method run() which needs to be implemented in the class which wants to create a Thread.


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }


How do you code to inherit from an interface in java?

We have been using references to the term "Implementing an Interface" throughout the preceding chapters and we havent yet actually dug deep into this topic. As you might remember, an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior. When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not) Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class. Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile: public class Ball implements Convertible { // Keyword 'implements' public void openHood() { } public void setOpenHoodFactor(int bf) { } } Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all… Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following: • Provide concrete (nonabstract) implementations for all methods from the declared interface. • Follow all the rules for legal overrides. • Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method. • Maintain the signature of the interface method, and maintain the same return type (or a subtype). • It does not have to declare the exceptions declared in the interface method declaration.

Related questions

How do you add import packages to a java program in netbeans?

package thisPackage; import otherPackage.*; class myClass { }


Purpose of marker interface in java?

Generally, they are used to give additional information about the behaviour of a class.It is just used to "mark" Java classes which support a certain capability . Examples: java.util.RandomAccess java.io.Serializable java.rmi.Remote java.util.EventListner javax.servlet.SingleThreadModel java.lang.Clonable javax.ejb.EnterpriseBean


Which exception is thrown by the add method of Collection interface in java?

exception


What is the way to get rid of Java lang no class deffound error?

To get rid of the Java lang no class deffound error, one must add the class or .jar file which contains this class into the Java classpath. When a Java class is run from the command line, one must add a dot (.)


Why instance in abstract or interface?

we can make object of interface but in abstract we can not make object of it interface ab= new Classs(): in interface we maintain multiple inhetence by use of obj of interface we if inherit two class have same fun then we give the name of that interface and call the pertucular that fun interface ab= new class() ab.add(); but in Astract Class we cannot make object of it only class class wich inherit it can make object class ab2= new Class(); and by obj we call function of drived class ob2.add();


Why do you need runnable interface?

A Runnable Interface is one that is used to create a Java Thread... A Thread can be created in two ways and using the Runnable Interface is one of them. Example: public class Test implements Runnable { public void run(){ .... } } The Runnable interface would have an abstract instance of the method run() which needs to be implemented in the class which wants to create a Thread.


How can i add an employee class and an account class to the console application projects?

You create a new file for each class, and define a class in each. In Java, you use the "class" keywoard.


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }


How do you code to inherit from an interface in java?

We have been using references to the term "Implementing an Interface" throughout the preceding chapters and we havent yet actually dug deep into this topic. As you might remember, an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior. When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not) Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class. Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile: public class Ball implements Convertible { // Keyword 'implements' public void openHood() { } public void setOpenHoodFactor(int bf) { } } Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all… Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following: • Provide concrete (nonabstract) implementations for all methods from the declared interface. • Follow all the rules for legal overrides. • Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method. • Maintain the signature of the interface method, and maintain the same return type (or a subtype). • It does not have to declare the exceptions declared in the interface method declaration.


How do you use jar file in java?

JAR stands for JAVA Archive File. You can archive a number of java class files into an archive and form a JAR. If you place the JAR in a particular location in the computer and then add the path to the JVM Class path, the JVM will be able to recognize references to classes that are present in the JAR.


Write a java program to maintain bank details using packages?

Use the Array Package. if you need help - add me on msn james_mc_123@hot dkjdkdjd mail.comp


How can add java code to java server page?

Scriplet.