#Calling A Class with a relation

1 messages · Page 1 of 1 (latest)

dim thornBOT
#

This post has been reserved for your question.

Hey @sacred kayak! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#
__**First Class**__

public class Creator {
    String username;
    String password;
    public Creator(String user, String pass){
        this.username = user;
        this.password = pass;
    }
}

**__Second Class__**

public class Checker {
    public Creator creator;

    public void ownerLogin(){

        if (this.creator.username.equals("Bob") && this.creator.password.equals("Password")){
            System.out.println("Successful Login");
        }
        else{
            System.out.print("Error");
        }
    }
}

__**Main**__

import java.util.Scanner;

public class Main {
    public static void main (String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your Username ");
        String newUsername = scanner.nextLine();

        System.out.println("Enter your Password ");
        String newPassword = scanner.nextLine();

        Creator owner = new Creator(newUsername, newPassword);

        System.out.println("Lets check your username and password");
        
    }
}

__**Question**__
So i want to call the ownerLogin Method from the Checker Class.

How do i do that?
glass hollow
#

so where do you want to override it?

sacred kayak
#

What do you mean over ride?

lapis remnant
#

cant you just do owner.ownerlogin(parameters or whatever)

#

oh wait no, my bad

#

i read the class names wrong

#

you would need to either make the ownerlogin method static and do Checker.ownerlogin(owner), owner being passed as a parameter or you would need to make a constructor and do something like:

Checker checker = new Checker(owner); // again, begin passed as a parameter
checker.ownerlogin();

and then requiring the owner as a parameter to the constructor:

public Checker(Creator creator) {
  this.creator = creator;
} 
#

i can try to explain it better if this all is confusing

lean night
#

????

dim thornBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.