0

Sorry for my bad english.

Hello i wondering me if i can acces to a parent from it's children. for exemple with a "A" object wherein I create a "B" . It's possible, does this method has disadvantages ? if so what are the solutions?

edit 1: in my case i have a main object which instantiate two objects, a object which handle a database and an other which create view but i want to erase view from database so can i put the main object in the constructor of the view creator class and use the main object function in oder to access to the database and erase the view ? or it is a practice to avoid ?

edit2: sorry for the error, i'm talking about object, not about classes

edit 3: here is an example made by @DaveHowes (thanks to him)

class A {

public void doSomething(){
 B b = new B(this);
}

public void doSomethingElse(){
   // Do something wondrous
}

}

Class B {

private A par = null;
public B(A parent){
  par = parent;
}

public void callMethodInParent(){
   par.doSomethingElse();
}
  }

P.S: I'm not talking about inheritance.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
Dan Snow
  • 127
  • 1
  • 11
  • 2
    Look into inheritance. – Sotirios Delimanolis Mar 26 '14 at 23:00
  • look for inheritance and (for java also look into access specifiers ) – jmj Mar 26 '14 at 23:00
  • You can always access non-static members from outside class scope provided you have an object and the members are public. – Mahesh Mar 26 '14 at 23:03
  • You can do super. – gravitas Mar 26 '14 at 23:04
  • I believe you are referring to nested classes and here is an [example from C#](http://stackoverflow.com/questions/1105955/c-sharp-nested-class-access-parent-member) and an [example from java](http://stackoverflow.com/questions/31201/how-do-you-get-a-reference-to-the-enclosing-class-from-an-anonymous-inner-class) – Uxonith Mar 26 '14 at 23:09
  • With no pseudo-code, example, or otherwise anything that can explain what you're talking about, it's impossible to answer your question. – vanza Mar 26 '14 at 23:10
  • OP is **not talking** about inheritance. – Jabberwocky Mar 26 '14 at 23:10
  • 1
    First, what are you trying to do? Second, did you want to do that in Java, C++ or C? Can you post a self contained example in whatever programming language you're actually trying to do this in? – Elliott Frisch Mar 26 '14 at 23:12
  • it's a general question concerning java,c and c++ programing – Dan Snow Mar 26 '14 at 23:24

3 Answers3

1

I'm not sure I fully understand your question, but here goes .....

If you mean that in Class A, you instantiate an instance of Class B, then Class B will not have access to the data and methods of Class A unless you pass a reference to an instance of Class A into Class B. This is often done in the constructor of Class B

This contrived example illustrates what I mean

class A {

    public void doSomething(){
     B b = new B(this);
    }

    public void doSomethingElse(){
       // Do something wondrous
    }

}

Class B {

   private A par = null;
   public B(A parent){
      par = parent;
   }

   public void callMethodInParent(){
       par.doSomethingElse();
   }
}
DaveH
  • 7,187
  • 5
  • 32
  • 53
  • yes, i'm talking about that, ok thanks, so it's not a deprecated method ? – Dan Snow Mar 26 '14 at 23:21
  • 1
    If you mean is it bad practice? then I'd say "No" - in my experience it's quite a common thing to do. If you have a lot of objects and you find yourself glueing them together like this though, then it might be time to rethink your design. – DaveH Mar 26 '14 at 23:26
0

I believe you are referring to nested/inner classes and here is an example from C# and an example from java

Community
  • 1
  • 1
Uxonith
  • 1,602
  • 1
  • 13
  • 16
0

It sounds like you might be talking about inner classes. Such that

public class Outer {

    private class Inner {

    }
}

In this case, the inner class can see all members of it's parent, and vice versa.

public class Outer {

    private String id = "123c";

        private class Inner {

        public void useID() {
            String newID = id; //this works
        }
    }

An inner class is meant to be an extension of it's outer class. When a task contains a sub-task great enough for it's own object, but is still very tightly tied the main object, it can be put into an inner class which helps add a layer of separation. Since an inner class is an extensions of it's outer class, they rely on being able to see each other's members regardless of visibility.

Julian
  • 1,665
  • 2
  • 15
  • 33