Results 1 to 3 of 3
  1. #1
    bmL
    bmL is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Access child methods from parent class

    Let's say I have for example:
    PHP Code:
    abstract class A {
      
      public A() {
        B b = newB();
        b.test();
      }
      abstract class B {
          void test() {
    
           }
      }
    }
    Class B extends A {
      class B extends A.B {
         void test() {
            // i want to access this from parent class A
         }
      }
    }
    A = new A;...
    Is it possible to access child methods from parent class?

    EDIT: sorry not method but child class so:
    PHP Code:
    abstract class A {
      
      public A() {
        B b = newB();
      }
      abstract class B {
    
      }
    }
    Class B extends A {
      class B extends A.B {
         void A.B() {
            // i want to access this from parent class A
         }
      }
    }
    A = new A;...
    when i run this the extended part is not executed i get instance of parent inner class
    Last edited by bmL; 07-29-2012 at 01:41 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Access child methods from parent class

    Nope; and in your example you can't even create an A because class A is abstract. Why do you want such artificial class hierarchies? Is it an assignment?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bmL
    bmL is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Access child methods from parent class

    I want one abstract class which implements most of functionality so specialized classes need only to implement specialized things. Right now im doing something like this:
    PHP Code:
    abstract Class A {
    
       addNotify() {
          add(getB());
       }
       abstract B getB();
       abstract Class B {
    
       }
    }
    abstract class C extends A {
       
       B getB() {
          return new B();
       }
       class B extends A.B {
    
       }
    }
    and it does just fine. But i would like to get rid of the getB method and keep it functioning so I dont have to reimplement the A.addNotify just to call add(new B()); in every specialized class.

Similar Threads

  1. Please help me. Parent and Child Class with Swing
    By paul6lazo in forum AWT / Swing
    Replies: 22
    Last Post: 09-07-2011, 05:37 PM
  2. Access Parent and child node
    By emyk in forum Advanced Java
    Replies: 0
    Last Post: 05-02-2011, 11:16 PM
  3. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 PM

Posting Permissions

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