Results 1 to 6 of 6
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Why cant I use methods from the superclass?

    Ok so I included thenewboston tutorials as part of my resources and a strange problem occured as I was following along with his code

    Java Code:
    package beginner;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    
    public class EventHandlerProgram_54 extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	
    	private JTextField item1;
    	private JTextField item2;
    	private JTextField item3;
    	private JPasswordField passwordField;
    	
    	public EventHandlerProgram_54()
    	{
    		super("The title");
    		setLayout(new FlowLayout());
    		
    		item1 = new JTextField(10);
    		add(item1);
    		
    		item2 = new JTextField("enter text here");
    		add(item2);
    		
    		item3 = new JTextField("uneditable", 20);
    		item3.setEditable(false);
    		add(item3);
    		
    		passwordField = new JPasswordField("mypass");
    		add(passwordField);
    		
    		TheHandler handler = new TheHandler();
    		item1.addActionListener(handler);
    		item2.addActionListener(handler);
    		item3.addActionListener(handler);
    		passwordField.addActionListener(handler);
    	}
    	
    	private class TheHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			//create empty string and change it to what they want
    			String string = "";
    			
    			//if they touch box 1 and enter
    			if( event.getSource() == item1)
    			{
    				// get the text from that location
    				string = String.format("field 1: %s", event.getActionCommand());
    			}
    			else if( event.getSource() == item2)
    			{
    				string = String.format("field 2: %s", event.getActionCommand());
    			}
    			else if( event.getSource() == item3)
    			{
    				string = String.format("field 3: %s", event.getActionCommand());
    			}
    			else if (event.getSource() == passwordField)
    			{
    				string = String.format("password field is %s", event.getActionCommand());
    			}
    				
    			JOptionPane.showMessageDialog(null, string);
    		}
    	}
    	
    }
    and

    Java Code:
    package beginner;
    
    import javax.swing.JFrame;
    
    public class EventHandlerProgramTest_54 
    {
    	public static void main(String[] args)
    	{
    		EventHandlerProgram_54 application = new EventHandlerProgram_54();
    		
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ----- error
    		
    	}
    }
    I get the error The method setDefaultCloseOperation(int) is undefined for the type EventHandlerProgram_54

    now I dont understand...

    application is an object of EventHandlerProgram_54
    EventHandlerProgram_54 extend JFrame

    doesnt application get all of JFrames methods?

    it suggests I cast application as a JFrame like ((JFrame)application) but this has never happened with me before... surely you dont have to cast a subclass into the superclass if a subclass inherits all the methods

    theres something weird going on here. any ideas?

  2. #2
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why cant I use methods from the superclass?

    found the problem apparently this caused it

    private static final long serialVersionUID = 1L;

    I have no knowledge of what this variable is, I just eclipse generate some default value to remove the yellow...

    so what is this variable about anyway?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why cant I use methods from the superclass?

    Instead of making Eclipse add a 'default' serial version id, make it add a 'generated' serial version id (it's in the quick-fix section).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why cant I use methods from the superclass?

    will do, thanks!

    still not sure what it is though but I guess it must be unique

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why cant I use methods from the superclass?

    Start reading the API documentation for the Serializable interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why cant I use methods from the superclass?

    Or use an annotation that states that you just don't care about serialization.

    What's happening is that you're extending a class that implements the Serializable interface, and part of this interfaces "contract" is that classes that implement it should have a private static final long serialVersionUID field, one that is used by the serialization mechanism to see if the serialized objects being restored match the type of class that they are being assigned to (I think), that they are "compatable". Since there is very little chance that you will need to or want to serialize objects of this class, just tell the compiler that you don't care about this with the @SuppressWarnings("serial") annotation.

Similar Threads

  1. Replies: 11
    Last Post: 03-05-2012, 11:18 AM
  2. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  3. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  4. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •