Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
Java: The Public Class Main 4 30
Selenium docs api java index 3 48
hashmap order 17 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 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