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);
}
main
function is not a concept from OOP.