Здесь я хочу получить доступ к этой защищенной переменной rollno
в моем другом пакете package2
. Итак, ребята, я поставил обе программы: Итак, когда я запускаю Check.java
, он выдает ошибку not defined
@
Protected1
, единственная цель здесь - получить доступ к защищенной переменной из пакета test в 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);
}
}