Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help Let\'s look at a collection of classes and see how access levels aff

ID: 3539787 • Letter: P

Question







Please help

Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related. Classes and Packages of the Example Used to Illustrate Access Levels The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them. Figure B. An illustration of Java's access modifiers Objectives: Design and implement a Java application that tests the visibility of the members of the Alpha class as shown in the table in Figure B. Implementation: Implement your design by converting them into Java classes. Your class definitions should exhibit good object-oriented programming practice, especially those discussed in Chapters 8 and 9, such as data hiding using set() and get() methods, proper access modifiers, etc. Hand in the source programs.

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();
     }
}