Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: class extending combining with references

  1. #1
    Isty is offline Member
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    5
    Rep Power
    0

    Default class extending combining with references

    Hi ya'll!

    Suppose I have the following classes:

    Java Code:
    public class A extends B{
    	C c1;
    	public A(){
    		c1 = new C();
    		somethingB();
    	}
    }
    Java Code:
    public class B {
    	public B(){
    
    	}
    	
    	public void somethingB(){
    		c1.anythingC();
    	}
    }
    How can I access the object c1 from the class B? Do I have to pass a reference this way?
    Java Code:
    somethingB(c1);
    and
    Java Code:
    public void somethingB(C c1){
    		c1.anythingC();
    	}
    Or are there other solutions? Thanks for replying!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: class extending combining with references

    This seems terribly disjointed and terribly wrong. The base class should have no knowledge at all about any child class and certainly should have no dependencies on a child class. You need to rethink your design completely. One possible solution is to use a dependency injection though.

    I'm no pro at this, but something like this:

    Java Code:
    class Parent {
       public void doSomething(Injectable myInjection) {
          myInjection.myMethod();
       }
    }

    Java Code:
    class Child extends Parent {
       Injectable myInjectable;
    
       public Child() {
          myInjectable = new Injectable() {
    
             public void myMethod() {
                System.out.println("inside of my method");
             }
          };
          doSomething(myInjectable);
       }
    }
    Java Code:
    interface Injectable {
       void myMethod();
    }
    Last edited by Fubarable; 11-22-2011 at 12:34 AM.
    Junky likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: class extending combining with references

    Though even that looks skunky, especially a class calling a virtual method inside of a constructor like that. More often you'd see something like this:

    Java Code:
    public class Foo {
       public static void main(String[] args) {
          Injectable inject = new Injectable() {
             public void myMethod() {
                System.out.println("in myMethod");
             }
          };
          
          Parent parent = new Parent(inject);
          parent.doSomething();
       }
    }
    
    class Parent {
       Injectable myInjectable;
       
       Parent(Injectable myInjectable) {
          this.myInjectable = myInjectable;
       }
       
       public void doSomething() {
          myInjectable.myMethod();
       }
    }

Similar Threads

  1. Replies: 1
    Last Post: 12-17-2010, 10:24 AM
  2. Replies: 20
    Last Post: 06-03-2009, 06:33 PM
  3. Replies: 18
    Last Post: 11-22-2008, 06:25 PM
  4. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 10:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •