Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Pratham Patel
Pratham Patel
4,976 Points

What is the point of learning object oriented programming

In the java objects course we model apes dispenser We take two files Why couldn't we just do it on one file instead

1 Answer

With smaller java projects you could very well just put all of your code into one file. The reason behind using multiple files, besides just good habit/practice, is so that if you were creating a larger project with a lot of code and extra functionality, your one file with the "main method" doesn't get cluttered up and your code is easier to understand.

If this is confusing I can try to explain it in a different way.

Pratham Patel
Pratham Patel
4,976 Points

like your answer. When I first started object oriented programming I thought only about objects when the course teaches you the language such as methods and classes. But if you wouldn't mind I would like you to explain it in a different way

Here, let me try and give you an example and let's see if that helps you out a bit more.

Say we're making a hangman game. We would need like 3 main components: The main class(This class will only hold our main method, which is the entry point for a pure java source project), A Prompter class(This class will do the prompting to the user to guess the letter and what not), and finally a Hangman logic class(This class will hold the logic that actually makes the game playable after the user is prompted for a letter).

Technically, like I said in my previous post, we can put all 3 main components inside just one file and call it Hangman.java or something, but instead we would put each of the 3 main components in their own respective classes, so the one file that holds the main method doesn't get cluttered up.

In this example don't worry about the names of the methods etc... This is just to give you an idea of what I was talking about earlier.

Main.java

public class Main {
    public static void main(String[] args) {
        //once we create the other 2 classes for the hangman game
        //we would call the start method here, that would start the game for the user like:
        HangmanLogic hangman = new HangmanLogic(); //here we're creating a new instance of our HangmanLogic class so we can call the startHangman method off of it
        hangman.startHangman(); //this would be our made up method in our hangmanlogic class that starts the hangman game
    }
}

Prompter.java

public class Prompter {
    //in this class we would put all the code that is required to prompt the user to guess a letter and what not.

    private void promptMethod1() {
        //made up method for the prompter class
    }

    private void promptMethod2() {
        //made up method for the prompter class
    }

    private void promptMethod3() {
        //made up method for the prompter class
    }

    //and this class would be kind of like this. Do you get the point?
}

HangmanLogic.java

public class HangmanLogic {

    private void startHangman() {
        //this would be the method that like starts the hangman game
        //we would call this method in our Main class inside of our Main method(The entry point to our java program)
    }

    private void hangmanLogic1() {
        //helper method for hangman logic
    }

    private void hangmanLogic2() {
        //helper method for hangman logic
    }

    private void hangmanLogic3() {
        //helper method for hangman logic
    }

    //we would create many more helper methods to actually make hangman work
}

In all of these classes just imaging instead of blank methods that we had tons of code. You can already see how it would make our files less cluttered and much easier to follow along with and read, if you wanted to say add additional functionality to our hangman game.

Let me know if this makes things any less confusing for you as to why it's beneficial to put your logic into separate classes instead of just one huge one. :)

Pratham Patel
Pratham Patel
4,976 Points

Thanks it really cleared it up

No problem, glad to help. Happy coding :D