2

This is a pretty elementary general question, but it's pretty simple. I understand that you create a class to create objects, but I'm wondering if that is the ONLY reason why you would need to create a separate class? The reason I ask is because in a course I'm taking, all the students presented on the design of a program that we all had to build individually. Many students had 5-10 classes, while I only had 2. Both options seem to work just fine.

If this is a subjective question, then what is the most accepted way to write programs. What do industry leaders say about adding extra classes that might not exactly be 100% necessary?

Harrison Bergman
  • 43
  • 1
  • 1
  • 8
  • Sometimes a class is created which contains only static util methods. In this case, we might never instantiate the class. Also, a singleton class is also not instantiated (except the first time it gets accessed). Really, there is no hard and fast rule about to use classes in OOP. – Tim Biegeleisen Dec 12 '16 at 07:35
  • Main Reason is, Separation of Code into small pieces, which will give you following advantages; Readability, Usability, maintainability. and also help you while you are doing debugging. and you also can take advantages of features of OOP such as Inheritance, polymorphism, etc. – Faraz Sultan Dec 12 '16 at 07:45

3 Answers3

2

Here are some guidelines:

Single Responsibility Principle

The single responsibility principle basically says that each class should only do one thing. If there is a class that handles two or more things, split that into multiple classes.

So check your classes now, is there any class that does a lot of things, like getting user input, doing caluclations, and printing the result? If that's the case you probably want to split that into multiple classes.

Abstraction

Abstraction in OOP is very important. It's basically the process of making real world things into classes or interfaces. For example, if you were doing a calculation app, you would have a Calculator class that does the actual calculation. You would also have a CalculatorWindow class that manages the window of the calculator, like listening for button events. This class can then give the user input to a Calculator object and have it calculate the result.

Design Patterns

There are a lot of design patterns out there. By looking at these design patterns, you can see how classes interact and hopefully you'll get when to create a new class.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

There is no one-size-fits-all answer to this.


If this is a subjective question, then what is the most accepted way to write programs.

Obviously, that question is subjective too. (You don't expect us to do a survey for you do you?)

What do industry leaders say about adding extra classes that might not exactly be 100% necessary?

You mean like, Bill Gates, Larry Ellison?

They probably don't say anything.

Sure, there are some people who will get up on their soap box and tell you with great authority that X Y Z is "best practice" or some such. But most of the time these pronouncements are based on (at best) anecdotal evidence / personal experience. In other words, they are really opinions dressed up as facts.


Bottom line: there is no single correct answer, so you learn:

  • what works best for you (and your team), and
  • it is not really worth worrying too much about it ... provided what you are doing "works for you".

(The two extremes that don't "work" are when the classes are so fine-grained that you spend all of your time writing and reading boilerplate, OR the classes are so large and complex that you can no longer understand them.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Different class are used to do different tasks. This reduces code duplication and increases code reuse. And this helps you to follow design pattern to solve some critical problem easily. This is good practice.

Here is a simple example.

To sort data.

//Without using multiple class
public class A{
  public static void main(String[] args){
     int array[] = {1,6,1,8,34,5};
     for(int i=0; i< array.length; i++){
        //your procedure to sort the array
     }
     //other operations;
     //Now you need to sort another new array (new_array[])
     int new_array[] = {1,6,1,8,34,5};
     for(int i=0; i< new_array.length; i++){
        //your procedure to sort the new_array
     }
  }
}

Here in this example we used two for loop to sort two different array. Now see a example with multiple classes

public class A{
  public static void main(String[] args){
     int array[] = {1,6,1,8,34,5};
     
     int my_array[] = Opertaion.sortArray(array);     

     //other operations;
     //Now you need to sort another new array (new_array[])
     
     int new_array[] = {1,6,1,8,34,5};
     int my_new_array[] = Opertaion.sortArray(new_array); 
  }
}

public class Opertaion{
   public static int[] sortArray(int[] array){
     for(int i=0; i< array.length; i++){
        //your procedure to sort the array
     }
     return array;
   }
}

The above example is very simple example but when you need to do big projects using multiple class will reduce your time to code.

Suppose when you are in a big project

you will write a class to control database queries, a Service class to handle other operation with that database class, a controller class to control everything etc.

Community
  • 1
  • 1
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24