Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: abstract class:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default abstract class:

    After all this time OOP just clicked, I am laughing at myself like a mad scientist. I just needed a project to really required it I suppose. Reading about design patterns was a really cool moment for me. However I have questions:

    I have a abstract class like this below:

    public abstract class Weapon {
     
    	protected String name;
    	protected int zeroRoundCount;
    	protected int qualRoundCount;
    	protected int practiceRoundCount;
     
     
    	 	JFrame frame;
    		JButton enterBtn;
    		JButton closeBtn;
     
    		JLabel zeroLbl;
    		JLabel practiceLbl;
    		JLabel qualLbl;
    		JLabel calulateLbl;
     
    		JTextField zeroTextBox; 
    		JTextField practiceTextBox;
    		JTextField qualTextBox;
    		JTextField calculateTxtBox;
     
    	 Weapon(String name)
    	 {
    		 zeroRoundCount=18;
    		 qualRoundCount=40;
    		 practiceRoundCount=60; 
    		 String weaponName = null;
     
    		 	frame = new JFrame(weaponName);
    			frame.getContentPane().setLayout(new GridLayout(0,1));
    			frame.setSize(240,220);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    			//button will calculate total rounds needed
    			enterBtn =  new JButton("Enter");
    			closeBtn = new JButton("Close");
     
     
    			//text box's will be default values from abstract Weapon
    			zeroTextBox= new JTextField(5); 
    			practiceTextBox= new JTextField(5);
    			qualTextBox= new JTextField(5);
    			calculateTxtBox= new JTextField(10);
     
    			//creating labels
    			zeroLbl= new JLabel("Zero");
    			practiceLbl= new JLabel("Practice");;
    			qualLbl= new JLabel("Qual");
    			calulateLbl = new JLabel("Total Ammo Needed");
     
    			frame.add(zeroLbl);
    			frame.add(zeroTextBox);
     
    			frame.add(qualLbl);
    			frame.add(qualTextBox);
     
    			frame.add(practiceLbl);
    			frame.add(practiceTextBox);
     
    			frame.add(calulateLbl);
    			frame.add(calculateTxtBox);
     
    			frame.add(enterBtn);
    			frame.add(closeBtn);
    			frame.setVisible(true);
    	 }
     
     
    }

    I make another class:

    package army.strac;
     
    public class M4 extends Weapon{
     
    	M4() 
    	{
    		super("M4");
     
    	}
     
    }

    Then a main class and it runs. Now how can I put the actionListners methods:

    //example
    zeroTextBox.addActionListener(new ActionListener)
    	 {
    		 public void actionPerformed(ActionEvent ae)
    		 {
    			 zeroTextBox.setText(zeroRoundCount);
    		 }
    	 }

    I want to put them in the abstract class set them all up and simply change the field numbers values in the subclass. Therefore for every weapon type I just change the number required by accesses the field numbers in the constructor and BAM I am done.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: abstract class:

    If you want to use different values for every subclass of a class you can simply define an abstract method in the super class and have it implemented in each subclass.
    For example:

    public abstract class Person {
     
    	public abstract int getIncome();
     
    }
    public class Professor extends Person {
     
    	public int getIncome() {
    		return 2500;
    	}
     
    }
    public class Student extends Person {
     
    	public int getIncome() {
    		return 0;
    	}
     
    }

    As you can see, every class (Student / Professor) that subclasses Person can define its own implementation for the getIncome() method.
    A professor has an income of 2500 while a student has an income of 0.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: abstract class:

    No no.. I completely get this. I want each subclass to have a JFrame, rename it and change the field values. Since I am using swing I need to create actionListeners. I want to create them in the abstract class and those methods logic will be closely coupled with the field names. When I try to do this I am getting a syntax error. The post cf code on post 1 has the example I am talking about.

    update:

    Just in case I was not clear. This following code:

    zeroTextBox.addActionListener(new ActionListener)
    	 {
    		 public void actionPerformed(ActionEvent ae)
    		 {
    			 zeroTextBox.setText(zeroRoundCount);
    		 }
    	 }

    Wads placed in the abstract class and it didn't work, so I tried to put it in the M4 class, same thing. I can do:

    public void actionPerformed(ActionEvent e) {
     
    		zeroTextBox.setText("18");
    		practiceTextBox.setText("60");
    		qualTextBox.setText("40");
    	}

    And the works but doesn't really do anything at the moment. I want to handle each text box in their own method and be able to subclass this logic from the abstract class if possible.
    Last edited by jocdrew21; September 3rd, 2014 at 01:32 PM.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: abstract class:

    You never create nor add any listeners in the code you have posted so its hard to really tell what you are trying to do.
    Since your variables are package-private you should have access to them as long as you declare your subclass in the same package, so I dont see a problem with that. Other then bad design of course.

    The preferred solution would be to create and add the listeners in your super class but have them call abstract methods which would need to be implemented by the subclasses.

    For example:
    public abstract class AbstractWindow extends JFrame {
    	private static final long	serialVersionUID	= 1L;
     
    	public AbstractWindow() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
     
    		JPanel contentPane = new JPanel();
    		setContentPane(contentPane);
     
    		JButton btn = new JButton("Some Button");
    		contentPane.add(btn);
     
    		btn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				btnAction();
    			}
    		});
    	}
     
    	protected abstract void btnAction();
     
    }
    public class ActualWindow extends AbstractWindow {
    	private static final long	serialVersionUID	= 1L;
     
    	protected void btnAction() {
    		System.out.println("Button had been pressed!");
    	}
     
    }

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: abstract class:

    YES!!! That is what I was trying to do.

    Bad design? Is it a good design to put each class into their own package? This is for modularity reasons correct?

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: abstract class:

    No, its not a good design to put each class into a separate package, that would be rather pointless.
    But its a bad design to have so many package-private variables, especially if they are not really needed. Furthermore, having a class that creates GUI elements in its constructor, especially a JFrame, is just calling for bad things to happen later down the road.
    But you have to make those experiences yourself.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: abstract class:

    would you care to enlighten me? I only mentioned the classes being in every class because of this framework I was learning "OSGI", it requires it for modularity purposes.

    Where would you suggest I create my GUI. This is not the real program, this is me testing out a model after reading about design patterns.

    I am trying to make a program that calculates how many rounds a certain weapon needs per training event. There will be about 50 different weapons and but what they must accomplish is pretty much the same.

    My plan right now is to make a JFrame of JCheckBoxes with labels picking the weapons systems need to calculations for. After that those select weapons appear, enter the number of people, hit enter and it is all transferred to a beautiful excel sheet that is able to be e-mailed.

    This is my first real tackle at something real. I am very excited and will love any advice. I am still in the planning and designing process.

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: abstract class:

    Right now you would create a new JFrame for each of them, if you have 50 you would have 50 windows popping up.

    Usually people use the MVC design for applications with a GUI. You could read up on that, perhaps on wikipedia.
    The basic idea is that you split your program up into 3 parts. The model part where data is stored and altered, the view part that is the GUI and everything that is specific to the application itself, and the control part which manages interaction between the model and the view.

    It goes well with the observer pattern to have the view update when the model changes.

Similar Threads

  1. about Abstract class
    By sivarjun in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 3rd, 2012, 10:52 AM
  2. [SOLVED] Abstract method in a non-abstract class
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2012, 11:27 AM
  3. What's difference between public class and abstract class?
    By Java95 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 24th, 2012, 07:37 AM
  4. Abstract class
    By jeskoston in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:46 PM
  5. abstract class
    By robinglow in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2010, 01:36 AM