Solved

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

Posted on 2008-10-13
5
684 Views
Last Modified: 2013-11-23
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
Comment
Question by:bent27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 22706836
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 22706883
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
 
LVL 5

Expert Comment

by:muktajindal
ID: 22708432
How can you call "b.getC().getSample();" at the place where it is being called in your code ?
Please post the proper code.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22708807
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
 
LVL 2

Accepted Solution

by:
mkopka earned 125 total points
ID: 23146219
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 Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question