0

So I have class A extends class B, class B extends JInternalFrame, B has a method myMethod (in my case myMethod converts an order system to decimal numbers). Now I want to use myMethod from class B in a method in class A, but when I try to call super.myMethod() it says myMethod() is not found, and when I took a look at all the methods that show up after typing "super.", the methods are all JTable methods (if I control click any method from the drop down I go to JTable.java).

I have tried creating a B in class A then use B.myMethod(), this works but is there a way to call myMethod in class A without making a B?

public class B extends JInternalFram{ 
   constructor(){
   }
   public myMethod(){
      //does conversions here
   }
}


public class A extends B{ 
   constructor(){
   }
   public anotherMethod(){
      //needs to use b.myMethod();
   }
}

Thanks

  • 3
    Assuming you haven't overridden it, what's wrong with just calling `myMethod()`? – rgettman Oct 23 '13 at 17:49
  • It sounds like your environment is out of sync, refresh all your content and try again. also make sure that myMethid is public – Todoy Oct 23 '13 at 17:49
  • Obviously you didn't put the full code because the above would not compile. Make sure that myMethod is in the correct scope for class A to see it. – wxkevin Oct 23 '13 at 18:04

3 Answers3

0

Mmmmm Im not sure but arent you missing the return Type?

Try changing:

public myMethod(){//does conversions here}

With:

public void myMethod(){//does conversions here}

Remember:

In general, a method has the following syntax:

modifier returnValueType methodName(list of parameters) {
  // Method body;
}

Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.

Return Type: A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.

Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.

Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

Method Body: The method body contains a collection of statements that define what the method does.

JeremiasK
  • 3
  • 3
0

Hope this helps -

public class B extends JInternalFram{ 
   B(){
   }
   public void myMethod(){
      //does conversions here
      System.out.println("Inside myMethod of B");
   }
}


public class A extends B{ 
   A(){
   }
   public void anotherMethod(){
      //needs to use b.myMethod();
      myMethod(); 
   }
}
ajc
  • 1,685
  • 14
  • 34
0

First of all, use void as a return type in both Methods, otherwise JVM thinks it is a constructor. Back to the main problem, why don't you use super.myMethod()?

Bye, J

J.W.
  • 9
  • 2