OOP 1. Introduction to Classes and Objects
- Overview
- Classes combine data and methods.
- A class defines a data type.
- Advantage: Classes correspond to concepts in the problem domain.
- Advantage: Classes reduce complexity by increasing coherence and reducing coupling.
- Introduce classes to store data in value objects.
- Instance variable = Variable declared in class outside a method. Typically private.
- Field = Synonym for instance variable, attribute. Common informal term.
- Attribute = Synonym for instance variable, field. Often used in the design phase.
- Property = Synonym for instance variable, field. Common term if field is known publicly.
- Declaring fields (instance variables).
Object-Oriented Programming (OOP) concepts
Class = data + methods.
Everything (data and methods) in Java is contained in classes. So far you've been using
classes to hold methods (main
and perhaps a few other static methods).
These methods use local variables, which are completely private to the method,
and which disappear when the method returns.
Historical development. Classes can also be used to store data. Historically, programming languages had something similar to classes for grouping data, usually called structs or records. These were used for storing only data, not methods. Eventually, advantages of combining both data and the methods to work on that data were recognized.
Classes model problem. One advantage of encapsulating data and methods in a class is to make programming objects that reflect "objects" in the problem domain. If your problem deals with orders and products, then you'll very likely have classes called Order and Product.
Classes reduce complexity. Another advantage of classes is reducing complexity. Complexity limits the size and reliability of programs. Complexity is reduced by increasing cohesion (putting things together that belong together) and reducing coupling (interconnections). The proper use of classes can make large improvements in both of these.
Class, object, OOP. The name class was almost universally adopted for programming language structure which combines data and methods, object is used for each instance of a class that is created, and the practices that developed around these ideas is called Object-Oriented Programming (OOP).
Value objects - Data-first approach. We'll start with classes that represent data, and then add constructors and methods. Ultimately you will be more concerned with methods than data, and you'll see how (and why) methods are used to completely hide the data implementation.
A class stores the attributes (data) of something
Group values. Classes are used to group a number of related, named, data values associated with an entity. By entity we mean something that is typically a noun when you are talking about a problem you are solving. For example, in a university computing system, you might have a class to represent a student, an instructor, a classroom, a department, a course, a section of a course, ....
Student example. The information you would store about students, would be their name, id, etc. We'll keep this example simple and just save name and id information.
Declare each field.
A class contains declarations of the fields (instance variables, attributes) that hold the
data associated with a student.
These declarations are like declaring a local variable in a method,
except that you will also specify the visibility.
We'll start by using the visibility modifier public
, which lets anyone see them.
For example,
the following Student1
class can be used to represent a student.
As you go through these notes, we'll make improvements to this class.
public class Student1 { public String firstName; public String lastName; public int id; }
A class is a type
Noun. Classes are usually given the name of a noun, and
the fields (variables) in it are attributes associated with that noun.
Predefined Java classes that you have already
used are String
, which contains the characters in the string and the length,
JOptionPane
, which contains information necessary to produce a dialog box
on the screen, etc. These predefined Java classes are used for the programming
infrastructure that you need.
Many common infrastructure classes are available in the 3000+ predefined Java library classes.
But, of course, these don't address your problems.
Business objects. Most classes you define will concern your problem domain, where they represent business objects (Student, TimeOfDay, Order, ...).
Declaring business object variables is like declaring any other variable.
Use the class name as a type, just as you've used String
, int
, etc.
For example, the following declares two Student1
variables.
Student1 bestInClass; // This variable references a Student1 object. Student1 t; // And here is another
These don't have values yet, but we'll do that in the next section.
Does your problem require classes or simple variables?
If the data you are working with, for example a temperature,
can be expressed as a simple value and the operations on it are
all defined by operators, then just use a simple variable for it (int
, double
, String
, ...).
Often the data associated with an entity in your problem is not a simple value,
but has multiple attributes.
Examples showing classes as groups of attriutes.
- Instead of working with simple temperature numbers, you might have TemperatureMeasurement objects, which have several attributes: the temperature, the time, and the location.
- A Student class would represent information about a student: name, id, address, ... Each object of this class would have a data about a specific student.
- A Classroom class would have information about a classroom's building, room number, capacity, whether it has a projector, etc.
Object-Oriented Design involves deciding which classes you need to represent the things (entities) in your problem. For simple programs, you often use only simple variables, but as programs get larger, you will define more and classes to represent the things you are working with.
Similarity to database table
If your are familiar with databases, a class is very similar to a table definition. If you're not familiar with databases, don't worry because these lessons don't assume you are.
Review Questions
[TODO]