Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default When should I separate classes?

    I'm going through a Java text book. So far in chapter 6 (which focuses on classes and objects), they've been using two separate classes. One has methods and constructors, and another related class demonstrates them in action. Here's an example:

    Java Code:
    public class Rectangle
    {
       private double length;
       private double width;
    
       /**
        * The first constructor accepts no arguments, and assigns 0.0 to the length
        * and width fields:
        */
    
       public Rectangle()
       {
          length = 0.0;
          width = 0.0;
       }
    
       /**
        * The second constructor (overloaded constructor) accepts two arguments
        * which are assigned to the length and width fields:
        */
    
       public Rectangle(double len, double w)
       {
          length = len;
          width = w;
       }
    
       /**
        * The setLength method accepts an argument
        * which is stored in the length field. 
        */
    
       public void setLength(double len)
       {
          length = len;
       }
    
       /**
        * The setWidth method accepts an argument 
        * which is stored in the width field.
        */
    
       public void setWidth(double w)
       {
          width = w;
       }
    
       /**
        * The set method accepts two arguments    
        * which are stored in the length and width
        * fields. 
        */
    
       public void set(double len, double w)
       {
          length = len;
          width = w;
       }
    
       /**
        * The getLength method returns the value   
        * stored in the length field.  
        */
    
       public double getLength()
       {
          return length;
       }
    
       /**
        * The getWidth method returns the value    
        * stored in the width field.
        */
    
       public double getWidth()
       {
          return width;
       }
    
       /**
        * The getArea method returns the value of the 
        * length field times the width field.
        */
    
       public double getArea()
       {
          return length * width;
       }
    }

    And here's the separate class that demonstrates both of the Rectangle class's constructors:


    Java Code:
    public class TwoRectangles
    {
       public static void main(String[] args)
       {
          // Declare two Rectangle variables, but don't
          // create instances of the class yet.
    
          Rectangle box1, box2;
    
          // Create a Rectangle object and use the
          // first constructor.
    
          box1 = new Rectangle();
          System.out.println("The box1 object's length "
                             + "and width are "
                             + box1.getLength() + " and "
                             + box1.getWidth());
    
          // Create another Rectangle object and use
          // the second constructor.
    
          box2 = new Rectangle(5.0, 10.0);
          System.out.println("The box2 object's length "
                             + "and width are "
                             + box2.getLength() + " and "
                             + box2.getWidth());
       }
    }
    Unless they're just doing that to make the constructors and methods easier to understand, why is it a good idea to make them separate like that? Why not just merge the Rectangle and TwoRectangles classes together as one class? If you ask me, it looks more like a cheap way of using inheritance or something?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: When should I separate classes?

    You have separate classes when it makes sense. So you would have a Triangle class, a Circle class etc separate from the Rectangle class. this is common sense as each class represents a distinct type of object.

    A common approach is to have what is called a driver class. This is the class that contains the main method as the entry point and controls the direction the program takes. Of course you can place the main method in your Rectangle class but it is often cleaner to keep it separate. Especially when your program contains 3, 4, 100 classes. Which one should the main method go in? If I gave you a program containing 100 classes could you find which one has the main method? Another thing to keep in mind is that each class can have its own main method but this is usually done for testing purposes only. You would still only want a single main method as the entry point.

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

    Default Re: When should I separate classes?

    Maybe you should read Fowler's refactoring; it's a good read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: When should I separate classes?

    Note that when you are in that learning stage the examples that you are exposed to tend to be way too simplistic and unrealistic to really get the whole separation of concerns that this revolves around. I find that when I'm working on an actual real life project, it is usually a pretty natural process of putting everything in its rightful place because what you're working on makes sense.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2012, 11:31 PM
  2. Replies: 4
    Last Post: 09-15-2012, 08:41 AM
  3. Classes in separate files
    By natdizzle in forum New To Java
    Replies: 1
    Last Post: 07-25-2012, 06:35 AM
  4. Event Handling with Separate Classes
    By newbie123 in forum New To Java
    Replies: 9
    Last Post: 05-26-2011, 06:12 AM
  5. Replies: 1
    Last Post: 11-18-2008, 05:48 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
  •