[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 696
  • Last Modified:

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
0
bent27
Asked:
bent27
  • 3
1 Solution
 
sciuriwareCommented:
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!
0
 
sciuriwareCommented:
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!
0
 
muktajindalCommented:
How can you call "b.getC().getSample();" at the place where it is being called in your code ?
Please post the proper code.
0
 
sciuriwareCommented:
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!
0
 
mkopkaCommented:
Others above me have shown / talked about super classes and extending a,b and c. Another way of dealing with this and accessing class A from class C (I assume based on the comment, because you wish to set a value of ID in class A from inside class C???) could be by providing references to the parent classes.

Ive attached a code snippet, removed your code to simplify and just shown what occurs and how to setup so that each class has access to its parent. Also, by providing :

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

You are basically providing a way for class B to know about its children. I have simply done the reverse and provided a way for Class C to know about its parent B and class B to know about its parent A. Your List method and my parent method (if implemented across all classes in the hierarchy) would allow you to walk up or down the hierarchy in either way from top to bottom.

i.e.: using the snippet below you could do:

    A top = new A();
    B mid = new B(top);
    C bottom = new C(mid);

and then after this you could do the following calls:

   bottom.getParent();    //which would return you mid (the B class)
   mid.getParent();    //which would return you top (the A class)

or even:

    bottom.getParent().getParent(); //to go straight from the bottom to the top level parent at the top of the tree hierarchy

and hence you could work your way up the hierarchy from C class to the A clas son top. This could of course be extended across as many classes as you want.

I hope that this is what you are looking for (it was a bit hard to determine from the code snippet you gave)
public class A{
}
 
public class B{
    private A parent;      <---- added a parent reference
    public B (A a) {       <-- added a constructor with A class parent
        this.parent = a;
    }
    public A getParent() {   <--- returns parent of B class
        return this.parent;
    }
}
 
public class C{
    private B parent;    <-- added parent of C class
    public C (B b) {     <-- added constructor that takes in parent
        this.parent = b;
    }
    public B getParent() {   <--- returns parent of C class
        return this.parent;
    }
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now