Classes and Object Oriented Programming (OOP)
What is Object Oriented Programming
Briefly, Object Oriented Programming (OOP) is a way of organizing
the code in a program. It is the process of defining classes that are
used to create objects which are used to perform all of the actions of
the program. For example, in the Kennel program we created classes called
Kennel and Dog. Then we created PitBull, Cocker, and Chihuahua as sub-classes
of Dog. When the program ran it created a Kennel object using our Kennel
class as a blueprint. Next we created one object each of PitBull, Cocker,
and Chihuahua and let each of these objects handle the action of "barking".
In the GUI (Graphical User Interface) demonstration programs each widget
(text box, button, list box, combo box, label, menubar, menu, menu item, etc.)
was created as a separate object. All of these objects then worked together
to implement the program. Java is a programming language that is entirely
object oriented. Other programming languages, like C++, Python, etc., do not
require you to use OOP, even though most programmers do.
Some important terms related to OOP
-
Class -- A class is a source code file which the programmer
writes that provides the "blueprint" for creating an object.
Examples of classes in the programs created in this course are
Kennel.java, Dog.java, PitBull.java, Cocker.java, Chihuahua.java,
JavaGuiDemo.java, GUIDemoMenuBar.java, etc. When compiled each of
these source files becomes a .class (Kennel.class, Dog.class,
etc.) file from which the program can create objects when running.
There are also many classes included in the Java library. Examples
are the GUI classes of JTextField, JLabel, JButton, etc.
-
Object -- What is created when the program is running using
the classes as "blueprints" or guides.

Figure 1: Classes and Objects
-
Instantiate -- (verb) The process of creating an object
from a class "blueprint" using the new operator.
For example: Kennel kn = new Kennel(); This creates a reference
variable called kn which can reference an object of type
Kennel. Then, new Kennel() actually creates
(instantiates) the Kennel object and sets kn referencing it.
-
Instance -- (noun) The object that is created using the
new operator. In the example above of create a Kennel
object we say that kn is an instance of the class
Kennel.
-
Member (instance) variables -- These are variables that
are created as part of a class which can be accessed and used
by any method (function) in the class. The code defining member
variables is usually placed between the opening brace ({) and
the constructor method of a class file.
-
Member methods (functions) -- These are blocks of code
in a class that define what a class does. Each consists of
an access specifier (public, private, or protected), a
return type, a method name, a list of arguments to the function
enclosed in parentheses, and an opening ({) and closing (})
brace. All of the code for the function is placed between the
opening and closing braces. For more details see below.

Figure 2: Layout of a member method (function)
-
Constructor -- This is a special method included in all
class files. The method has the same name as the class, but
has no return type. When you instantiate a class in a program
using the new operator this function gets called
automatically in the new object. It is here that you do all
the set up and initialization needed to get this object ready
to be used in your program.
-
Inheritance -- The ability to create a class that is a
child class of another class. We say that the child
class extends the parent class (Figure 3). The child class then
inherits from its parent all member variables and
member methods from the parent class which has an access
specifier of public or protected, but not those that are
private. For example, in the Kennel project PitBull, Cocker,
and Chihuahua were sub-classes of Dog so they inherited the
m_iSize, m_sBreed, and m_sName member variables from Dog
as well as the member method bark(). Each of the sub-classes
however, overrode the bark() method with their own
version of bark().

Figure 3: Class PitBull is a sub-class of Dog, i.e. it
extends Dog
How class files are organized
A class consists of the header (public class MyApplication) in the
image below, an opening brace, a list of member variables, a constructor
method (function), any number of other methods (functions), and a closing
brace. There must be one and only one class in a program that also
contains a main() function in the format shown below. All main()
should really do is instantiate one instance of the class it is in. A Java
program may consist of more than one class.

Figure 4: Layout of a class file
Creating instances of classes using the new operator
In code when you want to create an instance of a class you use the new
operator followed by what looks like a call to the class constructor of
the class with appropriate arguments. In figure 5 below the new operator
is used to create three instances of the class Dog. Each instance is passed
three arguments: an int specifying the size of the Dog, a String giving
the "type" of Dog, and a String giving the name of the Dog.

Figure 5: Using new to create instances of classes
The Java Library
There are many, many classes built-in to Java that you can use in
your programs. All of these are listed with complete descriptions of all
the member variables and methods at the Java web site.
Click
here
to visit the library.

Figure 6: A wealth of classes can be found in the Java Library