Link to home
Start Free TrialLog in
Avatar of bent27
bent27

asked on

Composition in java : access super-class from sub-class

class A{

B b = new B();

getID(){
return id;
}

b.getC().getSample();

}

class B{
List<C> cList = new ArrayList<C>();
}

class C{
getSample(){
if() {
//pick up value from properties file
// format the value and parameter substitute it with 'Id' from A
}

}
}


I need to know how to access class A from class C

TIA
Avatar of sciuriware
sciuriware

You can't

the call    super() brings you 1 level higher only.

However, you can call a method in A that is not overloaded in B or C
from an object or when it is static.

;JOOP!
Correction: your example has no super classes or sub classes.

In this example, the getId in A is inaccessible because it is not static
and there is no object from A.

Your question title assumes:

public class C extends B extends A
{
// or such.

Resume: nothing in A is accessible because nothing is declared static
and there is no object:

A a = new A();


Can you tell us what this program would be intended for?
;JOOP!
How can you call "b.getC().getSample();" at the place where it is being called in your code ?
Please post the proper code.
Indeed b.getC().getSample(); is not in a method!

All 'active' code should be in methods or static blocks.
The latter looks:     static { <code> }

Your program:

class A                                            // Should be public class ....
{
   B b = new B();                             // Inaccessible, as it is not static.

   getID()                                        // Type missing, 'id' missing. inaccessible
   {
      return id;
   }

   b.getC().getSample();                // Code outside method, there is no 'getC()'
}

class B                                           // only accessible from A, cList is accessible
{
   List<C> cList = new ArrayList<C>();
}

class C                                           // Only accessible from A and B
{
   getSample()                               // Type missing
   {
      if()                                           // No condition
      {
//pick up value from properties file
// format the value and parameter substitute it with 'Id' from A
      }
   }
}

In short: this is no program, but a quick write-up.
Please provide us a complete program or a written plan.
;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of mkopka
mkopka
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial