6 A. Create a package, Add the necessary classes and import the package in java class.

 


Step 1: Define package and create a class


Program:


package data;

public class Demo

{

public void disp()

{System.out.println("Welcome to this class");}


public void square()

{

System.out.println("square of 2 is  :  4" );

}


}



Step 2:  Compile above class by using below command


>javac Demo.java


Step 3: again compile it for creating a directory


>javac -d . Demo.java


Step 4:  use this package inside different class


Program:


import data.Demo;


class AccessPackage

{

public static void main(String args[])

{

Demo d=new Demo();

d.disp();

d.square();

}

}


Step 5: compile and execute it


>javac AccessPackage.java

>java AccessPackage



Output:

Welcome to this class

square of 2 is  :  4


Comments

Popular posts from this blog

Program using Light Sensitive Sensors on Tinkercad

Custom Buttons in Android

Blink LED pattern using Arduino on Tinkercad