1 Reply Latest reply: May 31, 2008 3:32 AM by linkin RSS

    Understanding the combination of inheritance, packages and access modifiers

    807601
      I am working on a problem to help my practical understanding of accessing inherited members and methods from a different package using both inheritance and instance variable.
      Unfortunately, I am totally lost when I tried to set this up and understand what was happening.

      Problem:

      Create a class named "classA" under package "pack1" with 4 members (int pub_a, int priv_a, int prot_a, int def_a) and methods (pub_func1, priv_func1, prot_func1, def_func1). Mark the members and methods with 4 different access specifiers (public, private, protected, and default)

      Create a class named "classB" under package "pack2", ClassB will inherit classA of package pack1. ClassB will have 2 members (int pub_b, int priv_b) and methods (pub_func2, prot_func2) with 2 different access specifiers (public, protected)

      Try accessing the members of class classA in package pack1 inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through inheritance.

      Create an instance of class classA inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through the instance.

      Note: In your solution, comment out the lines that are in-accessible with the actual error message at the top of the comment.

      So I produced the following code (I don't think i set this up right):

       //filename A.java
      package pack1;
      
      public class A {
           public int pub_a;
           private int priv_a;
           protected int prot_a;
           int def_a;
      
           public int pub_func1() {return 1;}
           private int priv_func1() {return 2;}
           protected int prot_func1() {return 3;}
           int def_func1() {return 4;}
      }
       //filename B.java
      package pack2;
      import pack1.A;
      
      public class B extends A{
           
           public     int pub_b;
           private int priv_b;
      
           public int pub_func2() { return 5; }
           private int priv_func2() { return 6; }
           
           //directly inherited fields, at least one should be accessible?
           pub_a = 1;
           priv_a = 1;
           prot_a = 1;
           def_a = 1;
           
           int i;
           // directly inherited methods, at least one should be accessible?
           i = pub_func1();
           i = priv_func2();
           
           A a = new A();
           //not sure what's accessible here, as no error red underlines appear
           a.pub_a = 1;
           a.priv_a = 1;
           a.prot_a = 1;
           a.def_a = 1;     
      }
      I would be interested to know how one would approach and address this problem.
        • 1. Re: Understanding the combination of inheritance, packages and access modif
          linkin
          //filename B.java
          package pack2;
          import pack1.A;
           
          public class B extends A{
               
               public     int pub_b;
               private int priv_b;
           
               public int pub_func2() { return 5; }
               private int priv_func2() { return 6; }
               
               //directly inherited fields, at least one should be accessible?
               pub_a = 1;//true
               priv_a = 1;// false
               prot_a = 1;//true
               def_a = 1;//false
               
               int i;
               // directly inherited methods, at least one should be accessible?
               i = pub_func1();//true
               i = priv_func2();//false
               
               A a = new A();
               //not sure what's accessible here, as no error red underlines appear
               a.pub_a = 1;//true
               a.priv_a = 1;//false
               a.prot_a = 1//false;
               a.def_a = 1;//false
          }
          Edited by: fun_with_me on May 31, 2008 8:30 AM