Object Polymorphism

64
June 02, 2021, at 2:30 PM

I have a project for school that has a logIn and shows a table using a method that is corresponding with the account but when I log in with the other accounts it also calls the main class' table please help me, Im a first year IT student and this is my first project any help will be much appreciated.

Here's the source code: The LogIn:

public class LogIn implements ActionListener{
//GUI
 static JLabel userLabel;
 static JTextField userText;
 static JLabel passwordLabel;
 static JPasswordField passwordText;
 static JButton loginButton ;
 static JLabel success;
public static void main(String[]args) {    
    
    JFrame frame = new JFrame("Activities/Task Manager Log in");
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);  
    JPanel panel = new JPanel();    
    frame.add(panel);
 
    placeComponents(panel);
    frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
    panel.setLayout(null);
    userLabel = new JLabel("User");
    userLabel.setBounds(10,30,80,25);
    panel.add(userLabel);
    userText = new JTextField(20);
    userText.setBounds(100,30,165,25);
    panel.add(userText);
    passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(10,70,80,25);
    panel.add(passwordLabel);
    passwordText = new JPasswordField(20);
    passwordText.setBounds(100,70,165,25);
    panel.add(passwordText);
    loginButton = new JButton("Login");
    loginButton.setBounds(10, 110, 80, 25);
    loginButton.addActionListener(new LogIn());
    panel.add(loginButton);
   
    success = new JLabel("");
    success.setBounds(10, 140, 300, 25);
    panel.add(success);
    
}
@Override
public void actionPerformed(ActionEvent arg0) {
    String user = userText.getText();
    String password = passwordText.getText();
    if (user.equals("Rod Vince Tolosa")&& password.equals("123456") ) {
        success.setText("Login Successful!");
        Tolosa.showtable();
    }
    else if (user.equals("Paul Naethan Bustillo")&& password.equals("654321") ) {
        success.setText("Login Successful!");
        Bustillo.showtable();
    }
    else if (user.equals("Christia Anna Sarguet")&& password.equals("123123") ) {
        success.setText("Login Successful!");
        Sarguet.showtable();
    }
    else {
        JOptionPane.showMessageDialog(null, "Invalid Login Details");
        userText.setText(null);
        passwordText.setText(null);
    }
}

}

The tables class:

public class Tolosa {    
    
   public JFrame Tasks;    
    Tolosa(){    
    Tasks=new JFrame("Rod Vince Tolosa Tasks/Activities");    
        String column[]={"SUBJECT","ACTIVITY","DATE","TIME"};
        String data[][]={{"STS","Film Review","05/21/21","12:00 AM"},
                          {"Entrepreneurial Mind","Final Paper","05/15/21","4:30 PM"},    
                          {"Purposive Communication","Task 8","05/27/21","11:59 PM"}, 
                          {"GEPC","Poster Project","06/03/21","12:00 AM"},
                          {"InfoMan","Project Database","06/08/21","1:30 PM"},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""},
                          {"","","",""}};         
    JTable jt=new JTable(data,column);    
    jt.setBounds(30,40,200,300);          
    JScrollPane sp=new JScrollPane(jt);    
    Tasks.add(sp);          
    Tasks.setSize(650,400);    
    Tasks.setVisible(true);    
    Tasks.setLocationRelativeTo(null);
}    
    public static void showtable() {    
        new Tolosa();    
    }     
}
//Inheritance
class Bustillo extends Tolosa{
    public JFrame Tasks;    
    Bustillo(){    
    Tasks=new JFrame("Paul Naethan Bustillo Tasks/Activities");    
    String column[]={"COURSE SUBJECT","ACTIVITY","DEADLINE","TIME"};
    String data[][]={ {"Probability and Statistics","Final Quiz","06/3/21","4:30 PM"},    
            {"Entrepreneurial Mind","Final Paper","05/15/21","4:30 PM"},    
            {"Purposive Communication","Task 7","05/19/21","11:59 PM"}, 
            {"GEPC","Poster Project","06/03/21","12:00 AM"},
            {"Information Management","Final Project Presentation","06/08/21","1:30 PM"},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""},
            {"","","",""}};
JTable jt=new JTable(data,column);    
jt.setBounds(30,40,200,300);          
JScrollPane sp=new JScrollPane(jt);    
Tasks.add(sp);          
Tasks.setSize(650,400);    
Tasks.setVisible(true);    
Tasks.setLocationRelativeTo(null);
} 
//Polymorphism
public static void showtable() {    
        new Bustillo();    
    }     
}
Answer 1

It's because the method you're calling is static which doesn't work well with polymorphism. There's plenty of articles online and answers here on stackoverflow that discuss it (Few examples here, here, & here).

For a very simple example, try running this code and see what happens:

public class App {
    public static void main(String[] args) throws Exception {
        new B();
    }
}
class A {
    public A() {
        hi();
    }
    public static void hi() {
        System.out.println("hi");
    }
}
class B extends A {
    public B() {
        hi();
    }
    public static void hi() {
        System.out.println("hello");
    }
}
READ ALSO
How to check for available places with if statement?

How to check for available places with if statement?

I’m working on a reservation system for a camping websiteThere are several places where you can sit, but it shouldn't be possible to have 2 different family’s on one place

50
How to send a email from react (not a contact form)?

How to send a email from react (not a contact form)?

I want to automatically send an email with order details to the user when the order is placedI have only found tutorials, teaching how to enable the user to send an email using a contact form

63
SCSS preprocessing problem using keyframe names

SCSS preprocessing problem using keyframe names

I'm trying to use Scss preprocessor with animations and I stumbled across some kind of error or bugI know how to fix it, but but I want to know what's wrong and what exactly is causing this error

73