Java Basics
Lesson 1 - Introduction
- Introduce ideas of Object-Oriented Programming
- Defining a class with only instance variables.
Object-Oriented Programming (OOP)
Bottom-up approach. It is generally easier to understand classes by starting from the most basic idea - using classes to represent data, and then building up by adding constructors and methods. Ultimately you will be more concerned with methods than data, and these notes will show how methods can be used to completely hide the data implementation. We'll also put off the even higher-level issues of Object-Oriented Design, which is deciding which classes we need. Let's start at the bottom!
Class containing only data
Classes are most often used to group a number of related, named data values. For example, if you are storing information about students, you need their name, id, etc. A class describes the names and types of the data. For example, the following is the proposed definition of a class for a student. As you go through these notes, we'll make some improvements, but it shows some of the basic ideas.
public class Student1 { public String firstName; public String lastName; public int id; }
Class containing only methods
Some classes contain no data, only static methods work.
Every application that you write has
at least one static method in it, main
.
If you're not already familiar with these methods, see the
Static Method Tutorial.
Class containing both data and methods = Object Oriented Programming
Grouping related data with methods lets you not only model the values associated with business objects, but also the their behavior.
A class is a type
Classes are usually given the name of a noun, and contain the data
associated with that noun.
Some predefined Java classes that you have
used are String
, which contains the characters in the string,
JOptionPane, which contains the information necessary to produce a JOptionPane
on the screen, etc. These system classes are about programming, but the
classes you write will be mostly be about your problem domain
where they represent business objects (Student, TimeOfDay, Order, ...).
The class which contains main
is often an exception
to the noun-naming convention, and is typically named
after the problem you are trying to solve.
Problem domain classes
To solve your problem, you need to define classes to represent your data. Often, the data is not a simple primitive value. For example, if you are writing a program that does something with university information, you may define classes like Student, (class names start with an uppercase character), Course, etc. Each of these classes defines attributes, for example, the student name, etc.
Misc Notes
- An earlier term, Abstract Data Type (ADT) has largely been replaced by class.
- Not all classes are used to represent business objects. Some are used to serve as infrastructure to facilitate the programming process or solve other parts of the problem (eg, the GUI). The Java designers use the OO model extensively, which sometimes results in awkward constructions (eg, listeners) rather than adding language features.
Review Questions
[TODO]