Please help! Let\'s look at a collection of classes and see how access levels af
ID: 3539797 • Letter: P
Question
Please help!
Explanation / Answer
package one;
public class Alpha{
private int x;
public int y;
int z;
protected int a;
int getX()
{
return x;
}
int getY()
{
return y;
}
int getZ()
{
return z;
}
int getA()
{
return a;
}
void setX(int x)
{
this.x=x;
}
void setY(int y)
{
this.y=y;
}
void setZ(int z)
{
this.z=z;
}
void setA(int a)
{
this.a=a;
}
public void display()
{
//All variables can be accessed here
x=10;
y=20;
z=30;
a=40;
System.out.println("Variable X"+x);
System.out.println("Variable Y"+y);
System.out.println("Variable Z"+z);
System.out.println("Variable A"+a);
}
}
public class Beta{
public void display()
{
//Only public and default modifers are accesable here
//Private and protected can be accessed through getters/setters
Alpha alp = new Alpha();
alp.y=10;
alp.z=20;
alp.setX(30);
alp.setA(40);
System.out.println("Variable Y"+alp.y);
System.out.println("Variable Z"+alp.z);
System.out.println("Variable X"+alp.getX());
System.out.println("Variable a"+alp.getA());
}
}
--------------------------------------------------------------------------------------------------
package two;
import one;
public class AplhaSub extends Alpha{
public void display()
{
//Only public, protected and default modifers are accesable here
//Private can be accessed through getters/setters
Alpha alp = new Alpha();
alp.y=10;
alp.setZ(20);
alp.setX(30);
alp.a=40;
System.out.println("Variable Y"+alp.y);
System.out.println("Variable Z"+alp.getZ());
System.out.println("Variable X"+alp.getX());
System.out.println("Variable a"+alp.a);
}
}
public class Gamma{
public void display()
{
//Only publicare accesable here
//Private, protected and default modifers can be accessed through getters/setters
Alpha alp = new Alpha();
alp.y=10;
alp.setZ(20);
alp.setX(30);
alp.setA(40);
System.out.println("Variable Y"+alp.y);
System.out.println("Variable Z"+alp.getZ());
System.out.println("Variable X"+alp.getX());
System.out.println("Variable a"+alp.getA());
}
}
------------------------------------------------------------------------------------
import one;
import two;
public class TestOOP{
public static void main(String []args){
Alpha a=new Alpha();
System.out.println("Alpha");
a.display();
Beta b = new Beta();
System.out.println("Beta");
b.display();
AplhaSub as = new AplhaSub();
System.out.println("Alpha Sub");
as.display();
Gamma g = new Gamma();
System.out.println("Gamma");
g.display();
}
}