2 Replies - 9435 Views - Last Post: 21 March 2010 - 03:12 PM Rate Topic: -----

#1 darek9576  Icon User is online

  • D.I.C Lover

Reputation: 195
  • View blog
  • Posts: 1,619
  • Joined: 13-March 10

Writing programs using multiple classes

Posted 21 March 2010 - 02:50 PM

Hey there,

I have been programming for quite a while now, but ALL my programs where written in one class... and thats not very object oriented, isnt it? So my question is.. who do you write your code using multiple classes?.. what i mean by last sentence is, how do you combine them.. because although i can write several classes in one program, i dont know how to use them.. im only using the class with main method in it?
I heard something called composition, searched for it.. but didnt quite get it.. so therefore im here..

If someone has a good link or can explain how to make use of different classes or give an example i would really appreciate it..

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Writing programs using multiple classes

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 73
  • View blog
  • Posts: 539
  • Joined: 19-February 10

Re: Writing programs using multiple classes

Posted 21 March 2010 - 02:56 PM

So you would make a class with a bunch of methods to do the processing and such. Then just call these methods in a driver. For example:
public class ExampleClass
{
    public ExampleClass()
    {
        // constructor
    }

    public void methodOne()
    {
        System.out.println("In method 1");
    }

    public void methodTwo()
    {
        System.out.println("In method 2");
    }

    public void methodThree()
    {
        System.out.println("In method 3");
    }
}

public class Driver
{
    public static void main(String[] args)
    {
        ExampleClass ex = new ExampleClass();
        ex.methodOne();
        ex.methodTwo();
        ex.methodThree();
    }
}


There really shouldn't be much processing in the Driver class. It should mainly be where everything is started. Likewise, most, if not all the processing should be in another class by use of methods which are called in the Driver. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Writing programs using multiple classes

Posted 21 March 2010 - 03:12 PM

Check out our Java tutorials section. There are a ton of tutorials on classes, like Locke's tutorials on class design, my tutorial on Moving Away From Parallel Arrays which discusses class design, japanir's tutorial on Abstract classes vs. Interfaces, and others. Basically, the thing to remember is that classes are blueprints for Objects, and that the main() method should only be used be used to start the program. Honestly though, the best examples for using multiple classes are GUI programs, as you can't make GUIs in Java w/out multiple classes. Here's a basic example:
//JFrame is a class, and MyFrame is a class
class MyFrame extends JFrame{ 

   JPanel panel; //JPanel is a class
   JLabel label; //JLabel is a class
  
   public MyFrame(){
        panel = new JPanel();
        label = new JLabel("Hello world Frame!");
       
        panel.add(label); //panel contains label
        this.add(panel);  //this frame contains panel
 
        this.setSize(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
   }
}

class Main{ //Main is a class

   //notice how I just use the main() method
   //to start the program, not to put my logic into
   public static void main(String[] args){
         new MyFrame(); 
    }
}



Notice how I use 5 or 6 classes in this program to make it work, and the interactions between these classes. JPanel interacts with JLabel, the MyFrame class extends JFrame and interacts with JPanel, etc. When using multiple classes, they interact with each other in some way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1