Sunday, 12 March 2023

3. Create package pack1 within this package create class A which contains one instance variable and one instance method. Create another package pack2 within this package create class B. where class B is calling the method and variable of class A


/* save as a file 1 -> A.java */

package Pack1;

public class A
{
    public int var1 = 10;
   
    public void print()
    {
        System.out.println("this is pack1 in class A print method");
    }
}


---------------------------------------------------------------------------

/* save as a file 2 -> B.java */
package Pack2;
import Pack1.*;

class B
{
    public static void main(String args[])
    {
        A obj = new A();
       
        System.out.println("pack1 in class a instance variable value is: " + obj.var1);
        obj.print();
    }
}


No comments:

Post a Comment

python programs

1. sum of two number