Main Responsibilities in Object-Oriented Programming

entry-pointjavaobject-orientedprogramming practicessingle-responsibility

I'm new to object oriented programming and I don't understand what's the purpose of the main.

Yes, I read that it's the "entry point" of the program but what I don't understand is what should be in the main? And what are its responsibilities?

It may happen that something written in the main could be encapsulated in another object, but how much should you use this approach?

Here is my very first main I wrote in Java, it's very simple but it may make you understand my doubt better. I have an abstract class Animal which is extended by "Cat" and "Dog". I used the main to create some object and also as an "interface" with the user, indeed as you can see I used some conditional instruction to "ask the user" what he want to do.

My question arose from the fact that the interface could be encapsulated in another object and not giving that responsibility to the main.

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("What type of animal do you want to create? \n dog cat");
    String type = input.nextLine();
    if ( Objects.equals(type, "dog")){
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Dog first = new Dog(name, age);
    }
    else if ( Objects.equals(type, "cat")) {
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Cat first = new Cat(name, age);
    }

    else{
        System.out.println("Error: the specified type does not exist.");
    }
    System.out.println("The number of animals is:" + numberOfAnimals);
}

Best Answer

First off, your example isn't an object-oriented program. It's a procedural program that happens to store data in objects, because that's the tool that your language (Java?) provides for structured data.

A true object-oriented program consists of objects that interact with each other -- it's about behavior rather than data (I realize that is a controversial statement, so here's a link where you can see multiple definitions of object-orientation from people with more credentials than me; note that behavior appears in most of them).

In a true object-oriented program, according to the definition I use, you have independent objects interacting with each other. The role of the main function is to create the initial objects and wire them together.

As a simple example, consider a web-application that's built on top of a database. This application could be broken into objects in many ways, but here's one of them: a Networking object that accepts connections, parses the HTTP request, and dispatches to an appropriate Controller object, which interacts with a Database object and produces the response (if you'd like to associate one or more View objects with each controller, feel free to do so). You could also add a Threadpool object to provide separate streams of execution.

The role of main in this application might be to:

  1. Create the Database object
  2. Create all of the Controller objects, and associate them with the Database object
  3. Create the Network object, and associate all of the Controller objects with it.
  4. Start the Network object running (which might also involve creating the Threadpool and wiring it into the Network).

These setup steps could be explicitly specified in main, or they could be handled by some other object. For example, in a typical Spring application, all that the main function does is create the application context (a single object). This triggers creation and wiring of all the objects mentioned in the configuration for that application context.

Related Topic