0

I'm trying to code binary to decimal and decimal to binary using polymorphism in java however it keeps printing the method situating in the parent class and not the override. Thanks for the help.

OopFinal.class:

package oopfinal;
import java.util.Scanner;

public class OopFinal extends Convert{

    public static void main(String[] args) {

      Scanner userInput = new Scanner(System.in);

      System.out.print("Please value to convert: ");
      int inputNumbers = userInput.nextInt();

      Convert convert = new Convert();

      //checks whether number is binary or not  

      if (inputNumbers !=0 || inputNumbers % 10 > 1){
           String bin = Integer.toBinaryString(inputNumbers);
           int binaryResult = Integer.parseInt(bin);
           convert.Calc(binaryResult);

      } else if (inputNumbers == 0 || inputNumbers % 10 == 1) {
           String dec = Integer.toString(inputNumbers);
           int decimalResult = Integer.parseInt(dec);
           convert.Calc(decimalResult);  

      } else {
           System.out.println("Negative numbers are invalid here... ");
      }    
     }


     //binary to decimal
     @Override
     public void Calc(int n) {
         System.out.println("Decimal equivalent is: " + n);
     }    
 }

Converter.class:

package oopfinal;

public class Convert {

    //decimal to binary
    public void Calc(int n) {
        System.out.println("Binary equivalent is: " + n);
   }
}
ekiryuhin
  • 803
  • 4
  • 19
  • For method overriding the signatures (except the return type) must be different. – Arvind Kumar Avinash Mar 15 '20 at 13:34
  • 1
    This is not eligible to be achieved using method overriding. – Arvind Kumar Avinash Mar 15 '20 at 13:40
  • Does this answer your question? [When do you use Java's @Override annotation and why?](https://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – fuggerjaki61 Mar 15 '20 at 14:11
  • There is a problem your use of polymorphism because your convert instance is not an OopFinal since your are writing: ```Convert convert = new Convert();```. Instead you should write ```Convert convert = new OopFinal()```. That is how you can get the call to Calc method to be dynamically bound to the child method and not the parent one. – alainlompo Mar 15 '20 at 15:55

0 Answers0