Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 59
hibernate insert example 13 28
how to see all occupied ports on windows 10 laptop 15 64
mysql jsp example issue 32 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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