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);
}
}