Search code examples
javaswitch-statementoverridingextend

java extending or overriding switch cases


I'm in a java course so I'm trying to be a little vague with the code so I can learn but not cheat. The assignment is to take last week's program and extend functionality. Basically, I wrote a program where I'm using switch with two cases (not sure that's the best choice, but it's what I did), and I want to add more user options. So it currently allows input 'w', 'x', but I want to add 'y' and 'z' as options by extending class A with class B.

There is a default case in the class A that basically outputs "only enter 'w' and 'x'." The problem, is even with the new B class which extends it, it prevents anything but 'w' and 'x'.

I know I need the B class to override that so it allows w, x, y, and z, and then the default triggers when anything but those four options are entered. Either way, help please!

Below is class A (I got rid of some of the code for my question, but all the variables, user input and scanner work. It's the cases I'm having issues with):

import java.util.Scanner;
public class A {
public A()
{
  // define and implement variables
  // call scanner into existance to read inputs from the user
  // Ask for user input (abbreviated section) and store in variables

  oper = myManager.next(".").charAt(0);

  switch(oper)
    {
       // case w call to firstMethod method
       case 'w':
          DoSomething = firstMethod(num1,num2);
          System.out.println(" The result is "+FirstAns);
        break;
       // case w call to secondMethod method
       case 'x':
          DoSomethingElse = secondMethod(num1,num2);
          System.out.println(" The result is "+SecondAns);
        break;
            default:
            System.out.println(" Please Enter 'w' or 'x' only.");
      }
   /* note, this portion I got rid of some work, it's normally
      math related but modified here just to return characters for
      this post since I think it's irrelevant to my question (and I
      don't want to cheat) */

 static char firstMethod(char a)
 {
 return a;
 }
 static char secondMethod(char a)
 {
 return a;
 }
}
}

And below is class B which extends A, and I'm not able to convince to allow more cases. Note, after compilation, I'm executing B, but it's still only allowing the cases from A.

 import java.util.Scanner;
 public class B extends A {
 public B() 
 {
  // define and implement variables
  // call scanner into existance to read inputs from the user
  // Ask for user input (abbreviated section) and store in variables

  oper = myManager.next(".").charAt(0);

  switch(oper)
    {
       // case w call to firstMethod method
       case 'w':
          DoSomething = firstMethod(num1,num2);
          System.out.println(" The result is "+FirstAns);
        break;
       // case w call to secondMethod method
       case 'x':
          DoSomethingElse = secondMethod(num1,num2);
          System.out.println(" The result is "+SecondAns);
       break;
       case 'y':
          DoSomethingMore = thirdMethod(num1,num2);
          System.out.println(" The result is "+ThirdAns);
        break;
       // case w call to firstMethod method
       case 'z':
          DoSomethingLast = fourthMethod(num1,num2);
          System.out.println(" The result is "+FourthAns);
       break;
          default:
          System.out.println(" Please Enter 'w', 'x', 'y', or 'z' only.");
        }
      }
   // again, simplified this portion

 static char thirdMethod(char a)
 {
 return a;
 }
 static char fourthMethod(char a)
 {
 return a;
 }
 public static void main(String[] args) {
    B b = new B();
 }
}

I then update the test program to import class B (instead of the old program which imported A since B is supposed to extend A). But it still only shows cases from A. I know it's the order of operations on how the program loads the cases, just not sure how to fix it.


Solution

  • The default constructor of a superclass is always called first by the default constructor of the subclass.

    In your example, the class A constructor is called when creating class B with the default constructor.

    A solution is to move your logic into a method with the same signature in both classes and call that method in the constructor of the superclass.

    Something like this:

    class A {
        public A() {
            logic();
        }
    
        private void logic() {
            // Your switch of A
        }
    }
    
    class B extends A {
        public B() {
            super();
        }
    
        private void logic() {
            // Your switch of B
        }
    }
    

    Dynamic Binding is the OO-principle behind this solution.