Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Private Fields Question

  1. #1
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Private Fields Question

    Ok I know that a subclass cant inherit private fields from a superclass but im trying to build a subclass for my button listeners and i cannot access my array field of button because they are private how im suppose to inherit my array buttons field.....since fields should be usually private

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Private Fields Question

    Might want to look into the "protected" keyword instead of private.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Private Fields Question

    Quote Originally Posted by Luwine View Post
    im trying to build a subclass for my button listeners
    I'm almost certain that you have a bad design in mind, as you shouldn't ever need a subclass of the class that contains the button array for your listeners. Instead, you might want to have a class that implements ActionListener and add the same or separate instances of that class to each JButton.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Private Fields Question

    Quote Originally Posted by AndrewM16921 View Post
    Might want to look into the "protected" keyword instead of private.
    Good advice, but really shouldn't be applied for this particular scenario.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Re: Private Fields Question

    //Simple Calculator
    //Manuel Berrios
    //June 18, 2013

    import java.awt.*;
    import javax.swing.*;

    public class Hello_World extends JFrame
    {

    protected JButton[] button;
    private JPanel panel1;
    private JPanel panel2;
    protected JTextField textf;
    protected String word="second";

    public Hello_World()
    {
    word = new String();
    textf = new JTextField(100);
    button = new JButton[10];

    Panels_Buttons();
    Frames();
    }
    public void Frames()
    {
    setVisible(true);
    setSize(180,190);
    setTitle("Hello World");
    //setResizable(false);

    add(panel1, BorderLayout.NORTH);
    add(panel2);

    }
    public void Panels_Buttons()
    {
    textf.setComponentOrientation(ComponentOrientation .RIGHT_TO_LEFT);

    FlowLayout fl = new FlowLayout();

    panel1 = new JPanel(new BorderLayout());
    panel1.add(textf);

    button[0] = new JButton("1");
    button[1] = new JButton("2");
    button[2] = new JButton("3");
    button[3] = new JButton("4");
    button[4] = new JButton("5");
    button[5] = new JButton("6");
    button[6] = new JButton("7");
    button[7] = new JButton("8");
    button[8] = new JButton("9");
    button[9] = new JButton("+");

    panel2 = new JPanel(new BorderLayout());
    panel2.setLayout(fl);
    panel2.setBorder(BorderFactory.createLineBorder(Co lor.black));
    panel2.setComponentOrientation(ComponentOrientatio n.LEFT_TO_RIGHT);

    for(int i=0;i<10;i++)
    {
    panel2.add(button[i]);
    }

    }
    public static void main(String[] args)
    {
    new Hello_World();
    }

    }


    thats my main class

  6. #6
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Re: Private Fields Question

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class Listeners extends Hello_World implements ActionListener
    {

    public Listeners()
    {
    button[0].addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
    textf.setText(button[0].getText());
    }

    }


    thats my class for the listeners

  7. #7
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Re: Private Fields Question

    i can do it just in one class but i confused of when i need to separate files of .java or when i just do it in one file

  8. #8
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Re: Private Fields Question

    plus im confused of difference of object oriented and structured programming im want to program in object oriented....

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

    Default Re: Private Fields Question

    Quote Originally Posted by Luwine View Post
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Listeners extends Hello_World implements ActionListener
    {
    	
    	public Listeners()
    	{
    		button[0].addActionListener(this);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		textf.setText(button[0].getText());
    	}
    
    }

    thats my class for the listeners
    This is blatant mis-use of inheritance. You really don't want to do this as this is not what inheritance is for. You use it to create a more specialized class of the original class, and that is not what your purpose is.

    Instead make your fields private, consider creating a getButtons() method that returns the JButton[] array, and then using that method to allow outside classes to add ActionListeners to your JButtons. But whatever route you choose, the solution does not involve inheritance for this problem.
    Last edited by Fubarable; 06-23-2013 at 06:50 PM.
    DarrylBurke likes this.

Similar Threads

  1. Question on Singleton + Private Inner Classes
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 10:45 AM
  2. A question on Inheritance and fields
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 12-16-2011, 08:39 PM
  3. Replies: 5
    Last Post: 05-20-2011, 04:01 PM
  4. Arraylist errors with private fields
    By DJRome in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 12:00 PM
  5. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:08 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
  •