User Defined Package in Java

User defined package is created with the help of “package” keyword. Whereas to use a package we use the import keyword.

Demo.java

package abhi;

public class Demo

{

public void sum(int num1,int num2)

{

int result;

result=num1+num2;

System.out.println("the sum of two numbers is:"+result);

}

}

Tester.java
 

import abhi.Demo;

class Tester extends Demo

{

public static void main(String args[])

{

Tester obj=new Tester();

obj.sum(10,20);

}

}

Procedure to run the program:

First compile the Demo.java as follows:

For compilation Demo.java:

javac –d . Demo.java

Secondly compile the Tester.java in another Command Prompt.

For compilation Tester.java:

javac Tester.java

java Tester

Output

Image-1.jpg

Image-2.jpg

Ebook Download
View all
Learn
View all