DIALÓGICA CON EL BRÓCOLI Y LA EDUCACIÓN
4. PROGRAMA NARRATIVO EN PELÍCULAS RELACIONADAS CON LA EDUCACIÓN
Objects
Variables like ints or doubles can be thought of in two ways - data storage in memory, or as
representing quantities in the real world. Similarly we can think of objects in two ways. We can think of what they actually are in memory, and what the idea is, in terms of what they represent.
Objects in Java represent things. Here are some examples of objects
Objects Context
Buttons, windows, scroll bars, drop-down lists, combo boxes
GUI programming
Employees, managers, departments HR - personnel management Aliens, demons from hell, monsters Video game
Accounts, cheques, direct debits Banking
Goods for sale, customers, orders Sales management Files, directories, drives, computers File management Aircraft, flights, runways, air lines Air traffic control
Some objects represent real physical objects in the real world, such as aircraft. Others are in the virtual world of the computer, such as a file.
All objects are things. Objects are nouns.
Exercise
What objects would you expect to have in a program used to manage a veterinary clinic?
Objects in memory
As a Java program runs, objects are created and sometimes destroyed. They are held in memory.
The information held in each object is held in a block of memory.
Sometimes objects are made to persist by storing them in a file, so that they can be accessed again the next time the application runs.
What is in an object?
Objects have two kinds of members:
Fields. Sometiems called data members. These are pieces of data about the object needed by the program. For example, an employee would have a payroll number. A window has a title and a background colour. Data members might be primitive types like ints, or they might be objects themselves.
Methods. These are pieces of code which are blocks of instructions appropriate when an object does something. For example an employee might have a method called changeDepartment, which does what is needed when an employee moves to a different department. A window might have a
Classes
Imagine a window in a GUI program. The window might have four buttons on it. That's four objects.
But they are all buttons. They are all the same type. A class is a type of object.
Or:
Class Objects
Cat - The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felines and felids, is a small furry domesticated carnivorous mammal that is valued by humans for its companionship and for its ability to hunt vermin and household pests.
From Wikipedia, the free encyclopedia
A class is a type of object.
When a Java program creates an object belonging to a class, we say the object instantiates the class.
Or, the object is an instance of the class.
Class names start with a capital letter. Objects do not. So we might have a class called Cat, and an object, an instance of the Cat class, called tufty.
Classes objects and code
A Java application will use existing classes, creating objects which are instances of them.
But an application will also set up new classes. The programmer needs to specify what fields, methods and constructors the class has - in a class definition. Each class definition is in a different source code file, which has the same filename as the name of the class. So if we define a class called Cat, it must be in the file called Cat.java.
One of those class definitions must have a method called public static void main. This method is the point where the application will start execution :
Encapsulation
One aspect of OOP is the idea of encapsulation. The point of this is to 'seal up' the contents of an object, in order to ensure that the data in it cannot be corrupted by program bugs. The problem we are trying to fix is the possibility that bugs in a program might corrupt the data in an object and render it invalid. The solution is to ensure that wherever possible, the data members cannot be accessed from outside the class.
The solution is implemented by two ideas - access control and accessor methods.
Access control is achieved by the use of the keywords public and private (plus others). If a member is public, it can be accessed from anywhere. If it is private, it can only be accessed from inside the class. Most members, especially data fields, should be private. Sometimes they cannot be - for example
public static void main(String[] args)
has to be public, since it must be executed by the runtime system, outside the class.
However, we often need to be able to 'read' the values of data members - for example, finding out the width of a window. Sometimes we want to 'write' a new value - such as changing the width of a window. And we want to do this and still keep the data members private.
This is achieved by having public accessor methods. A getter method allows data members to be read from outside the class. These usually start with 'get' - such as getWidth. They are very simple.
To 'write' a new value to a private data field, we use a public setter method. An example would be setWidth. Such methods should include checks to ensure that the value being set is valid. For example, attempts to make the width of a window negative are ignored.
Exercise
1. What is an object?
2. Objects have three kinds of things - what are they?
3. How are classes and objects related?
4. How are classes and the source code files making up an application related?
5. What is the purpose of encapsulation?