1

I am doing an assignment for school in which I have to use examples of instanceof for casting. Currently,I have the following code

public class Visitor implements Actions{
    public Object buyTicket(Object visitor1){
        System.out.println("\nInput your age:");
        Scanner scanner = new Scanner(System.in);
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println("Input your budget:");
        int cash = Integer.parseInt(scanner.nextLine());
        if (age <18 && cash >=5){
            visitor1=new kid();
            System.out.println("You have bought a ticket");
        }
        else if(age >=18 && cash >=10){
            System.out.println("You have bought a ticket");
        }
        else{
            System.out.println("Not enough funds to buy a ticket");
        }
        return visitor1;
    }
    public static class kid{
        void print(){
            System.out.println("Legal guardian required for entry");
        }
    }
}

and I try to check after buying a ticket if I should call the print function thats used by the "kid" class by trying to check if visitor has become instanceof kid

    static void VisitorActions(){
        Visitor visitor = new Visitor();
        label3:
        while (true){
            Scanner scanner = new Scanner(System.in);
            String userChoice=scanner.nextLine();
            switch (userChoice){
                case "1":
                    visitor.buyTicket(visitor);
                    if (visitor instanceof Visitor.kid){
                        visitor.print();
                    }
                    break;
            //code continues to other cases

For some reason,I get the following error.

Inconvertible types; cannot cast 'com.projects.Visitor' to 'com.projects.Visitor.kid'

and also

Cannot resolve method 'print' in 'Visitor'

I checked around but could not find/understand a solution since I am new to java

Any help is appreciated :)

  • 3
    The only thing you assign to `visitor`in your `VisitorActions`method is `new Visitor()`, so what good do you think the `instanceof` check does? You can easily **know** what it is. Or do you expect `visitor.buyTicket(visitor)` to change what the value of `visitor` is? Because it can't: Java is always pass-by-value. Assgning a different value to a parameter won't change the variable that's been passed into the method. See [this question](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Joachim Sauer Apr 12 '21 at 21:21
  • @JoachimSauer this is interesting.What I tried to do was the second thing you explained,expecting visitor.buyTicket(visitor) to change what the value of visitor is,thanks for clearing this up for me! Since I cant change the value though,I am not sure how I should implement instanceof into my code,since if I make the change in main that would be a pointless check.What are your suggestions? – TheExtraSpicyBeef Apr 12 '21 at 21:32
  • Instead of trying to assign to a parameter, simply return an object and assign the return value of `buyTicket` to a variable. You can then use `instanceof` on that return value. – Joachim Sauer Apr 12 '21 at 21:49
  • Alright,got it,thank you for your help! – TheExtraSpicyBeef Apr 12 '21 at 22:11

0 Answers0