In this lesson you learned how to create and use objects such as SketchPad and DrawingTool. One can think of an OOP application as a simulated world of active objects. Each object has a set of constructors for its creation and a set of methods that can perform tasks. In the next lesson you will learn how to create your own classes.
Reflect on your understanding of these Lesson 1 ideas - if any of them is unclear, do something about it. Ask your teacher, search the Internet, watch a video, ask a friend or parent, etc.
A class is a blueprint that defines an object and can be used to create objects of that type. So far, you have created and used objects of type DrawingTool, SketchPad, and Color.
Ignoring special cases, classes have three important parts:
A class API tells you everything you need to know in order to create objects of that class and run its methods. The DrawingTool API is a good example.
Java includes thousands of built-in classes, many of which need to be imported because they're not inside the default java.lang package. The Color class is an example of this and is located in the java.awt package. To use the Color
class you need to import java.awt.Color
You can add external classes, such as gpdraw.jar, that do not come with Java.
To create and use an object, you must:
- Import the class, if needed.
Ex: import gpdraw.SketchPad;
- Declare an object of that class.
Ex: SketchPad pad;
- Create and initialize the object using a constructor
Ex: pad = new SketchPad(800, 600);
- Command the object using its methods
Ex: pen.drawRect(200, 100);
Program comments should be used to add clarity to your code
Ex: pen.move(578, -25); // Move to base of tree
The general process that leads to a running Java program is:
Every Java application starts execution in a main()
method.
Last modified: August 22, 2023
Back to Things To Know When You...