简体   繁体   中英

How can I call the overridden method in a child class from a parent class's inner class in JAVA?

So I have one parent class say A and an inner class of A called I. I is calling one function namely pop() which is class A's. Now this pop() is overridden by B which is a child of A.

Now when the pop is being called from I, will pop from A gets executed or B??

package testers; 
public class test 
{ 
  public void run()
  { 
    System.out.println("Inside test"); 
  }
  public void irun()
  {
    InnerClassOne o=new InnerClassOne();
    o.inside();
    System.out.println("Inside irun");
  }
  private void inside() 
  { 
    // TODO Auto-generated method stub
  } 
  class InnerClassOne 
  {
    void inside() 
    {
      System.out.println("Inside inner");
      test.this.run(); 
    } 
  } 
}


package testers; 
public class test2 extends test 
{ 
  public void run() 
  { 
    System.out.println("Overriden"); 
  } 
  public static void main(String []args)
  {
    test t1= new test();
    t1.irun(); 
  }
}

That completely depends on how you create the new instance of course. Let's say we have the following classes:

public class Test {

    public void print() {
        System.out.println("Hi");
    }

    public class TestInner {
        public void doSomething() {
            print();
        }
    }
}


public class Test2 extends Test {

    @Override
    public void print() {
        System.out.println("Bye");
    }
}

...

public class Main {

    public static void main(String[] args) {
        Test t1 = new Test();
        Test.TestInner testInner1 = t1.new TestInner();

        Test2 t2 = new Test2();
        Test2.TestInner testInner2 = t2.new TestInner();

        System.out.println("Test inner 1 doSomething: ");
        testInner1.doSomething();
        //Prints 'Hi'

        System.out.println("Test inner 2 doSomething: ");
        testInner2.doSomething();
        //Prints 'Bye'
    }
}

So dependenant on if you created an instance of Test or Test2 it just calls the print function of the outer class.

When you cast to TestInner or Test class you may see some unexpected behavior:

public class Main {

    public static void main(String[] args) {
        Test t1 = new Test();
        Test.TestInner testInner1 = t1.new TestInner();

        Test2 t2 = new Test2();
        Test2.TestInner testInner2 = t2.new TestInner();

        Test.TestInner testInner3 = (Test.TestInner) testInner2;
        System.out.println("Test inner 3 (cast inner) doSomething: ");
        testInner3.doSomething();
        //Prints 'Bye'

        Test t3 = (Test) t2;
        Test.TestInner testInner4 = t3.new TestInner();
        System.out.println("Test inner 4 (cast wrapper class) doSomething: ");
        testInner4.doSomething();
        //Prints 'Bye'
    }
}

Even though we explicity cast testInner3 (inner class) and testInner4 (outer class), it still prints Bye. That is because when you cast the object to another type, you just refer it using another type. The actual type of the object is not changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM