As a beginner I was writing a code which asks the burger buyer what they want in their burger. I did this by creating a burger object and modify its attributes by the user using a switch case method. Most of the attributes were Boolean variables. At the end of the order, what I want to do is to display all the attributes of the burger. i.e Your burger that consists of A meat, c bread, carrots, pickles, tomatoes are ready for order. But I could not think of a practical way of displaying such a result. I dont think I need to show the some-what awful looking code but here it is. I am open to any criticism. import java.util.Scanner;public class Main {public static void main(String[] args) {    System.out.println(What type of burger do you want? \n1)Basic\n2)Special 1 (Healthy) \n3)Special 2 (Deluxe));    Scanner scanner = new Scanner(System.in);    int choice = scanner.nextInt();    switch (choice) {        case 1:            BasicBurger basicBurger = new BasicBurger();            basicBurger.afterChoice();            break;        case 2:            SpecialBurger1 specialBurger1 = new SpecialBurger1();            specialBurger1.afterChoice2();            break;        case 3:            SpecialBurger2 specialBurger2 = new SpecialBurger2();            specialBurger2.afterChoice3();            break;        default:            System.out.println(Wrong Input);            break;    }}}BasicBurger Class:import java.util.Scanner;public class BasicBurger {private char breadRollType;private char meatType;private boolean lettuce;private boolean tomato;private boolean carrot;private boolean ketchup;private double price;public void afterChoice() {    setPrice(5);    System.out.println(Meat Type? (Available types = A, B, C));    Scanner scanner = new Scanner(System.in);    char choice = scanner.next().toUpperCase().charAt(0);    switch (choice) {        case 'A':            setMeatType('A');            increasePrice(1);            break;        case 'B':            setMeatType('B');            increasePrice(1);            break;        case 'C':            setMeatType('C');            increasePrice(1);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(You have selected  + getMeatType() +  type of meat.\n*********);    System.out.println(Bread Roll Type? (Available types = A, B, C));    Scanner scanner2 = new Scanner(System.in);    char choice2 = scanner2.next().toUpperCase().charAt(0);    switch (choice2) {        case 'A':            setBreadRollType('A');            break;        case 'B':            setBreadRollType('B');            break;        case 'C':            setBreadRollType('C');            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(You have selected  + getBreadRollType() +  type of bread roll.\n*********);    System.out.println(Tomatoes?\n1)Yes 2)No);    Scanner scanner3 = new Scanner(System.in);    int choice3 = scanner3.nextInt();    switch (choice3) {        case 1:            setTomato(true);            System.out.println(You have chosen to have tomatoes in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setTomato(false);            System.out.println(You have chosen not to have tomatoes in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Lettuce?\n1)Yes 2)No);    Scanner scanner4 = new Scanner(System.in);    int choice4 = scanner4.nextInt();    switch (choice4) {        case 1:            setLettuce(true);            System.out.println(You have chosen to have lettuce in your burger.\n*********);            increasePrice(1);            break;        case 2:            setLettuce(false);            System.out.println(You have chosen not to have lettuce in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Carrots?\n1)Yes 2)No);    Scanner scanner5 = new Scanner(System.in);    int choice5 = scanner5.nextInt();    switch (choice5) {        case 1:            setCarrot(true);            System.out.println(You have chosen to have carrots in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setCarrot(false);            System.out.println(You have chosen not to have carrots in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Ketchup?\n1)Yes 2)No);    Scanner scanner6 = new Scanner(System.in);    int choice6 = scanner6.nextInt();    switch (choice6) {        case 1:            setKetchup(true);            System.out.println(You have chosen to have ketchup in your burger.\n*********);            increasePrice(1);            break;        case 2:            setKetchup(false);            System.out.println(You have chosen not to have ketchup in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Your order is sent.);    System.out.println(Total price is  + getPrice() +  $\n*********);}public void increasePrice(double amount) {    setPrice(getPrice() + amount);}public void setBreadRollType(char breadRollType) {    this.breadRollType = breadRollType;}public void setMeatType(char meatType) {    this.meatType = meatType;}public void setLettuce(boolean lettuce) {    this.lettuce = lettuce;}public void setTomato(boolean tomato) {    this.tomato = tomato;}public void setCarrot(boolean carrot) {    this.carrot = carrot;}public void setKetchup(boolean ketchup) {    this.ketchup = ketchup;}public void setPrice(double price) {    this.price = price;}public char getBreadRollType() {    return breadRollType;}public char getMeatType() {    return meatType;}public boolean isLettuce() {    return lettuce;}public boolean isTomato() {    return tomato;}public boolean isCarrot() {    return carrot;}public boolean isKetchup() {    return ketchup;}public double getPrice() {    return price;}}Special Burger 1 class:import java.util.Scanner;public class SpecialBurger1 extends BasicBurger {private boolean pickles;private boolean cucumber;public void afterChoice2(){    setPrice(6);    setBreadRollType('B');    System.out.println(Meat Type? (Available types = A, B, C));    Scanner scanner = new Scanner(System.in);    char choice = scanner.next().toUpperCase().charAt(0);    switch(choice){        case 'A':            setMeatType('A');            break;        case 'B':            setMeatType('B');            break;        case 'C':            setMeatType('C');            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(You have selected  + getMeatType() +  type of meat.\n********* );    System.out.println(Pickles?\n1)Yes 2)No);    Scanner scanner2 = new Scanner(System.in);    int choice2 = scanner2.nextInt();    switch(choice2){        case 1:            setPickles(true);            System.out.println(You have chosen to have pickles in your burger\n*********);            increasePrice(0.5);            break;        case 2:            setPickles(false);            System.out.println(You have chosen not to have pickles in your burger\n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Tomatoes?\n1)Yes 2)No);    Scanner scanner3 = new Scanner(System.in);    int choice3 = scanner3.nextInt();    switch (choice3){        case 1:            setTomato(true);            System.out.println(You have chosen to have tomatoes in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setTomato(false);            System.out.println(You have chosen not to have tomatoes in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Lettuce?\n1)Yes 2)No);    Scanner scanner4 = new Scanner(System.in);    int choice4 = scanner4.nextInt();    switch (choice4){        case 1:            setLettuce(true);            System.out.println(You have chosen to have lettuce in your burger.\n*********);            increasePrice(1);            break;        case 2:            setLettuce(false);            System.out.println(You have chosen not to have lettuce in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Carrots?\n1)Yes 2)No);    Scanner scanner5 = new Scanner(System.in);    int choice5 = scanner5.nextInt();    switch (choice5) {        case 1:            setCarrot(true);            System.out.println(You have chosen to have carrots in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setCarrot(false);            System.out.println(You have chosen not to have carrots in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Ketchup?\n1)Yes 2)No);    Scanner scanner6 = new Scanner(System.in);    int choice6 = scanner6.nextInt();    switch (choice6) {        case 1:            setKetchup(true);            System.out.println(You have chosen to have ketchup in your burger.\n*********);            increasePrice(1);            break;        case 2:            setKetchup(false);            System.out.println(You have chosen not to have ketchup in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Cucumber?\n1)Yes 2)No);    Scanner scanner7 = new Scanner(System.in);    int choice7 = scanner7.nextInt();    switch (choice7) {        case 1:            setCucumber(true);            System.out.println(You have chosen to have cucumber in your burger.\n*********);            increasePrice(1);            break;        case 2:            setCucumber(false);            System.out.println(You have chosen not to have cucumber in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Your order is sent.);    System.out.println(Total price is  + getPrice() +  $\n*********);}public boolean isPickles() {    return pickles;}public boolean isCucumber() {    return cucumber;}public void setPickles(boolean pickles) {    this.pickles = pickles;}public void setCucumber(boolean cucumber) {    this.cucumber = cucumber;}}Special Burger 2 class:import java.util.Scanner;public class SpecialBurger2 extends BasicBurger {private boolean chips;private boolean drinks;public void afterChoice3() {    setChips(true);    setDrinks(true);    setPrice(7);    System.out.println(Meat Type? (Available types = A, B, C));    Scanner scanner = new Scanner(System.in);    char choice = scanner.next().toUpperCase().charAt(0);    switch (choice) {        case 'A':            setMeatType('A');            break;        case 'B':            setMeatType('B');            break;        case 'C':            setMeatType('C');            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(You have selected  + getMeatType() +  type of meat.\n*********);    System.out.println(Bread Roll Type? (Available types = A, B, C));    Scanner scanner2 = new Scanner(System.in);    char choice2 = scanner2.next().toUpperCase().charAt(0);    switch (choice2) {        case 'A':            setBreadRollType('A');            break;        case 'B':            setBreadRollType('B');            break;        case 'C':            setBreadRollType('C');            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(You have selected  + getBreadRollType() +  type of bread roll.\n*********);    System.out.println(Tomatoes?\n1)Yes 2)No);    Scanner scanner3 = new Scanner(System.in);    int choice3 = scanner3.nextInt();    switch (choice3) {        case 1:            setTomato(true);            System.out.println(You have chosen to have tomatoes in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setTomato(false);            System.out.println(You have chosen not to have tomatoes in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Lettuce?\n1)Yes 2)No);    Scanner scanner4 = new Scanner(System.in);    int choice4 = scanner4.nextInt();    switch (choice4) {        case 1:            setLettuce(true);            System.out.println(You have chosen to have lettuce in your burger.\n*********);            increasePrice(1);            break;        case 2:            setLettuce(false);            System.out.println(You have chosen not to have lettuce in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Carrots?\n1)Yes 2)No);    Scanner scanner5 = new Scanner(System.in);    int choice5 = scanner5.nextInt();    switch (choice5) {        case 1:            setCarrot(true);            System.out.println(You have chosen to have carrots in your burger.\n*********);            increasePrice(0.5);            break;        case 2:            setCarrot(false);            System.out.println(You have chosen not to have carrots in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Current price is  + getPrice() +  $\n*********);    System.out.println(Ketchup?\n1)Yes 2)No);    Scanner scanner6 = new Scanner(System.in);    int choice6 = scanner6.nextInt();    switch (choice6) {        case 1:            setKetchup(true);            System.out.println(You have chosen to have ketchup in your burger.\n*********);            increasePrice(1);            break;        case 2:            setKetchup(false);            System.out.println(You have chosen not to have ketchup in your burger. \n*********);            break;        default:            System.out.println(Wrong Input);            System.exit(0);            break;    }    System.out.println(Your order is sent.);    System.out.println(Total price is  + getPrice() +  $\n*********);}public void setChips(boolean chips) {    this.chips = chips;}public void setDrinks(boolean drinks) {    this.drinks = drinks;}public boolean isChips() {    return chips;}public boolean isDrinks() {    return drinks;}}
Viewing the final state of an Object
java;beginner;object oriented;inheritance
afterChoiceX isn't making use of polymorphism. Let's look at an example of how you can take advantage of polymorphism.public class BasicBurger {    public void afterChoice() {        // Gather options from user    }}public class SpecialBurger1 extends BasicBurger {    public void afterChoice() {        // Gather options from user    }}Notice that in SpecialBurger1, we simply override the method afterChoice.public class Main {    public static void main(String[] args) {        char choice = // Input from user        BasicBurger burger;        switch (choice) {        case 'A':            burger = new BasicBurger();            break;        case 'B':            burger = new SpecialBurger1();            break;        default:            // Exit            return;        }        // The next line will invoke the method on the        // appropriate class. This is polymorphism.        burger.afterChoice();    }}Base on this idea, we can implement our toString() in BasicBurger so that it encodes itself as a string.public class BasicBurger {    public void afterChoice() {        // Gather options from user    }    public String toString() {        String message = ' + getMeatType() + ' meat, '            + getBreadType() + ' bread;        if (lettuce) {            message += , lettuce;        }        if (carrot) {            message += , carrot,        }        // Do this for other possible ingredients of BasicBurger        return message;    }}public class SpecialBurger1 extends BasicBurger {    public void afterChoice() {        // Gather options from user    }    public String toString() {        // The super keyword invokes the method print        // of the closest parent that implements it.        // In our case, BasicBurger#print() will be invoked.        String message = super.toString();        if (pickles) {            message += , pickles;        }        // Do this for other possible ingredients of SpecialBurger1        return message;    }}By adding the following line after burger.afterChoice(), a burger's information will be printed to your screen.System.out.println(Your burger that consists of  + burger    +  is ready to order.);I hope this answer helped you understand more about polymorphism and inheritance.Note: I typed this during my lunch break and didn't test it.