Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to override a method in an inner class of the super class

807606Feb 22 2007 — edited Feb 22 2007
I have a rather horribly written class, which I need to adapt. I could simply do this if I could override a method in one of it's inner classes. My plan then was to extend the original class, and override the method in question. But here I am struggling.

The code below is representative of my situation.

public class ClassA
{
    
   ValueChecks vc = null;
   
   /** Creates a new instance of Main */
   public ClassA()
   {
      System.out.println("ClassA Constructor");
           
      vc = new ValueChecks();
      
      vc.checkMaximum();
     
     // I want this to call the overridden method, but it does not, it seems to call
     // the method in this class. probably because vc belongs to this class
      this.vc.checkMinimum();
      
      this.myMethod();
      
   }
     
   
   protected void myMethod()
   {
      System.out.println("myMethod(SUPER)");
   }
   
   protected class ValueChecks
   {
      protected boolean checkMinimum()
      {
         System.out.println("ValueChecks.checkMinimum (SUPER)");
         return true;
      }
      protected boolean checkMaximum()
      {
         return false;
      }
   }
}
I have extended ClassA, call it ClassASub, and it is this Class which I instantiate. The constructor in ClassASub obviously calls the constructor in ClassA. I want to override the checkMinimum() method in ValueChecks, but the above code always calls the method in ClassA. The ClassASub code looks like this
public class ClassASub extends ClassA
{
   public ClassAInner cias;
  
   
   /** Creates a new instance of Main */
   public ClassASub()
   {   
      System.out.println("ClassASub Constructor");
   }
   
   
   protected void myMethod()
   {
      System.out.println("myMethod(SUB)");
   }

   protected class ValueChecks extends ClassA.ValueChecks
   {     
      protected boolean checkMinimum()
      {
         System.out.println("ValueChecks.checkMinimum (SUB)");
         return true;
      }
   }
   
}
The method myMethod seems to be suitably overridden, but I cannot override the checkMinimum() method.

I think this is a stupid problem to do with how the ValueChecks class is instantiated. I think I need to create an instance of ValueChecks in ClassASub, and pass a reference to it into ClassA. But this will upset the way ClassA works. Could somebody enlighten me please.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2007
Added on Feb 22 2007
6 comments
694 views