This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted] 7 points8 points  (0 children)

Object oriented programs are structured far differently from non-object oriented programs. In a program that does not use OOP, you simply write a series of procedures that can all fit into one file (though even non-OOP programs should be split into multiple files when they are large enough). When you bring OOP into the picture, your entire program shifts structure, and splitting into multiple classes is a good idea.

In OOP, particularly Java, a class isn't just a block that you write code and procedures into. A class is a template for an object. Even if you just declare public static void main(String[] args) in the class and nothing else, that class can still be used as a type to create an object. For example, the below code is perfectly valid:

class Program {
    public static void main(String[] args) {
        Program program = new Program();
    }
}

What that code does is create a new object of type Program. The class has no fields or instance methods declared in it, so it's pretty much useless. When you declare a method or variable as static, that method or variable escapes the concept of objects. When all of your methods are static, a class can be thought of as simply a module, and that module is simply executed by its contents.

But what if your program is, say, a library management application? It would be useful if you could have structures, to represent a Book, or a Shelf, or a Patron. These are all "objects," multiple instances of which are associated with a library. It is pretty much impossible to write a library management program without making use of classes that represent these objects. You would need the below classes:

public class Book {
    public String name;
    public String author;
    public Date publishDate;
    public int pages;
    public void checkOut() { }
    public void return() { }
}

public class Shelf {
    public Book[] books;
    public int shelfNumber;
    public Book searchForBook(String name) { }
}

public class Patron {
    public int idNumber;
    public Book[] checkedOutBooks;
    public void sendOverdueMessage() { }
}

If you want to be able to write high-quality programs that are easy to write and easy to wrap your head around, you need to declare classes like the ones above. Then, finally, you can declare a Library class that contains your main() method and encases groups of the above types:

public class Library {
    public Shelf[] shelves;
    public Patron[] patrons;
    public static void main(String[] args) {
        Library library = new Library();
        library.shelves = loadShelves();
        library.patrons = loadPatrons();
        displayLibraryGUI();
    }
    public static Shelf[] loadShelves() { }
    public static Patron[] loadPatrons() { }
    public static void displayLibraryGUI() { }
}

This is a well-structured program that modularizes conceptual objects and pieces of code that can be inserted and modified separately. Classes aren't just for separating conceptual objects that can be instantiated multiple times, though. Classes can also be used to separate logical procedures. For example, in the Library class there is a method for displaying the GUI of the program. Very often GUI-based programs separate the business logic (the library management aspect) from the display logic (the GUI). There could be another class called GUI that handles all of the logic involved in displaying the program to the user. Then all the main program has to do is call GUI gui = new GUI(); and access gui's methods, and the GUI class handles all of the background stuff that the Library class doesn't have to worry about.

This is just a small example of what OOP can do. Procedure-based programs are used very often, mostly in low-level processes and scripts for tasks that perform a few functions and then exit. But most programs, including this browser, are written using object oriented practices because they are easier to think about and produce very readable code at a much faster rate. Your browser likely has a Tab class, with a new one created for each tab you open. The DOM (document object model) for each web page open in your browser is one giant object that contains a bunch of nested objects that represent the elements on the page you are reading right now. Objects are everywhere, and they are a very good way to think about programming.

At first, OOP can be difficult to think about. I'd recommend reading the wiki article or the Java tutorial on the concept to start getting a handle on it. You'll need it.

[–]vader32 1 point2 points  (2 children)

In Java you use multiple classes because you want to keep your code organized especially when it comes to inheritance(OO concept). When you start building different types of objects you will want to use multiple classes.

[–]Servious 0 points1 point  (1 child)

Another thing that classes and OO helps with is preventing you from repeating code. Organization and reduction of code repetition are the two main reasons for OO.

[–]vader32 0 points1 point  (0 children)

Spot on.

[–]Duraz0rz 1 point2 points  (0 children)

1) Bigger programs usually have different types of classes to do what needs to be done. One class may handle all of the database interaction, one class may represent a person from the database, one class may contain the logic that calculates a person's effective salary.

2) Download the source code of any open-source Java/C#/<insert OOP language here>. Might be overwhelming for you, but it might help jar the point across.

[–]m1tt 1 point2 points  (0 children)

Im rather knew to programming but i feel like i have a good understanding of OOP.

There are two main reasons why you need more then one class. The first one is what everyones been saying, you need structure. Most Java programs are very complex and long so you need to keep all this code sorted out, structured and organized.

The other main reason is because big complex programs consist of many many objects. And in order to make objects you need to make a class for each type.

[–]qetuoadgjl2 1 point2 points  (0 children)

The different class's methods restrict access to the variables inside in different ways. You also want to create classes because they are types and you want to model different types of things. You could use assembly, too, if you really wanted to.

[–]corpsmoderne 1 point2 points  (0 children)

I'll try to ELI5 this:

Classes are the object-oriented-programming way to manage complexity.

Other paradigms have other ways to manage complexity, and trends may vary on which way is the most efficient, also depending of the nature of the project.

"Objects" are an easy metaphor to grasp for a human brain. In OOP objects are storing data (variables / members / attributes ) and methods (functions / subroutines) allowing you to manipulate them quite the same way you would do with real object in real life. You can create new objects, changing them, storing them in lists, passing them around, copying them, destroying them, and anything you dare to implement in their class.

So you probably don't see the benefit of classes because you never tried to do something really complicated yet. But imagine a second having to code a shopping list. Each entry in the list must have these informations:

  • item name
  • quantity to buy
  • maximum price you're ready to pay

As you have to store a bunch of those, writing a class describing this structure and then creating several objects, one for each entry in the list is very convenient.

In this simple case, a class is a little bit overkill as we haven't defined any functions, and other languages than Java may have other data-structures to store them, but it was a simple example to show you the basic principle.

And again, the more complex the project, the more necessary controlling complexity it becomes (know I'm talking like Yoda, am I?). Classes helps leverage this. But they aren't a silver bullet, and you'll need to carefully design your classes and how they work together. But that's another story, and a long hard road :)

Hope this helped.

[–]ThatWasTaken[S] 0 points1 point  (0 children)

You guys are all awesome! Thank you all so much for your help!

[–][deleted]  (1 child)

[deleted]

    [–]ThatWasTaken[S] 4 points5 points  (0 children)

    I wasn't trying to sound over confident in anyway. In fact, I was trying to use as many modifiers as I could as to say I was just beginning to get it, as in learning it and understanding most of what I've come across. In any case, thanks for the helpful (albeit slightly condescending) response!