Home Topic 4

OOP - Enough For Now

Object Oriented Programming

OOP stands for Object Oriented Programming. It is an approach to programming, which give the programmer many advantages, primarily increased reliability of code, and more understandable organization of big projects.

Other approaches to programming may have object-like pieces, but it is the overall fundamental focus on objects that makes for the "object oriented" nature of OOP. Java was one of the first major programming languages to follow OOP principles.

Whereas you can think of primitive types, such as int and boolean, as being the fundamental building blocks of Java (like the basic cube shapes of Lego), you can think of objects as being all the other more complex tools available to use in your programming (like all of the specialized Lego pieces you can get, from roof pieces all the way up to pirate ships). Though it is important to realize that all objects in Java, no matter how complex, are, ultimately made up of primitive types like int and boolean.


Pre-existing Libraries of Objects

There are many, many standard object classes available to Java programmers, for example the System class' premade object out, which we have used from the very beginning to output our first Hello World, using:

System.out.println("Hello World!");

The objects is out. It happens to be an instance of the PrintStream class, which is, itself a part of the System class. It is public, which we have access to. And it, in turn, has a public method println( ), which we use to print out Hello World! We can think of System as being a library, and we have access to it whenever we want. There are lots of core Java libraries, and there are also lots of other libraries which programmers all around the world have made, and make available to others to use. So there are specialized libraries for making charts, and for doing data analysis, and pretty well anything else under the sun.

Or our Scanner object that we make in order to take in user input with console programs:

Scanner snr = new Scanner(System.in);
String s = snr.readLine( );

The Scanner object we made, and chose to name snr allows us to use all kinds of Scanner methods, including readLine( ) for reading strings from the console, where the user of the program can type words.

User Defined Objects

But we can also make our very own classes, and so "instantiate" objects of those classes. Most often, rather than classes that specialize in functionality, they are classes which represent the real-life objects that our programs work with. So, for example a Student class, and student objects. Or a Budget class, and budget objects, etc. etc. etc. We often term these kind of classes "template classes".

The template class itself is what is used to make individual instances of that class. So there would be one Student template class, but we would make 21 different, particular instances of Student objects. Here's an example:

Student s = new Student("Sally", true, 17, 12);
s.displayStudentDetails( );


Recognizing Objects

For now, you don't have to know the details of how to make new classes of objects, and how to use them. But you should be able to recognize when they are being used.

Take for example the ability for us to read in user input with Java's Scanner class, mentioned above. We make an "instance" of the Scanner class. And then we can access methods it has, like readLine( ) or nextBoolean( ).

Scanner snr = new Scanner(System.in); //making the instance of the Scanner class
String s = snr.nextLine( ); //using one of its methods
System.out.println("You just typed in" + s);

So, above, if the user ran that code, and typed "hello", the program would print: "You just typed in hello".

And, in fact, as noted above, the out of System.out.println( ) is actually a pre-made (PrintStream) object from the System class, which we can use for lots of things, including println( ).

(In class, get me to have us make our MySystem class, and MyPrintStream class, and add a static myOut instance variable of the MyPrintStream class to MySystem class. Give MyPrintStream a public myPrintln( ) method. And then....if you can still keep track of what's going on! call MySystem.myOut.myPrintln( ), and see how it works!!)

For now, whenever you see "dot notation" used, like with snr.nextLine( ), or System.out.println( ), you know you are using objects of another class. In other words, you are using functions or things available from other classes, which extend what you can do with Java way beyond basic usage of primitive data types.

Get into the habit of pausing whenever you press . to see what other functionality comes up on the list displayed by Netbeans or IntelliJ.