Here I want to access that protected variable rollno
to my another package package2
.
So guys I have put both programs:
So here when I run Check.java
it throws an error not defined
@
Protected1
the only objective here is to access the protected variable from package test to package2
package test;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Protected1
{
static protected int rollno;
static public String name;
static private int mobileno;
public void Protected1()
{
System.out.println("lets start doing the Assignment for Protected variable assigning in another package");
}
public static void main(String a[])
{
/*protected int rollno;
public String name;
private int mobileno;*/
Scanner br=new Scanner(System.in);
//Protected1 st=new Protected1();
try
{
System.out.println("Enter the name:");
name=br.nextLine();
System.out.println("Enter the rollno:");
rollno=br.nextInt();
System.out.println("Enter the mobile number:");
mobileno=br.nextInt();
System.out.println("lets just print it out:");
System.out.println("name is \t"+name);
System.out.println("rollno is \t"+rollno);
System.out.println("mobileno is \t"+mobileno);
}
catch(Exception e)
{
System.out.println("Exception caught");
e.getMessage();
}
}
}
package package2
import java.io.*;
import java.util.*;
import test.* ;
public class Check extends Protected1
{
public int Check()
{
System.out.println("hello guys ..lets try");
return 0;
}
public static void main(String a[])
{
Scanner dr=new Scanner(System.in);
Protected1 as=new Protected1();
int roll=as.rollno;
System.out.println("type welcome");
String input=dr.nextLine();
System.out.println("now we have to assess the protected variable from Protected 1 i.e"+roll);
}
}