Solved

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

Posted on 2008-10-13
5
642 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
scoresSpecial  challenge 13 40
pairstar challenge 2 41
Running Jira on Raspberry PI 2? 3 127
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now