Presentation on theme: "Classes, Objects, Methods, and the Java API ICOM4015, FALL 2014 A BRIEF INTRODUCTION (to be studied before Lab 2) CREATED BY KATYA I. BORGOS REVISED BY."— Presentation transcript:
Classes, Objects, Methods, and the Java API ICOM4015, FALL 2014 A BRIEF INTRODUCTION (to be studied before Lab 2) CREATED BY KATYA I. BORGOS REVISED BY AMIR H. CHINAEI
What are classes? In Java, everything goes in a class (for now). This is where you will type your source code. In other words, when you run your program, you are running a class. As you noticed already from Lab 1, this class contains a main() method. This is where your program starts running. Another thing you have seen already is the fact that one program can contain more than one class. However, no matter how many classes your program uses, one of those classes must contain a main() method—in order to run your program.
What are objects? In Lab1, we had a FrameViewer class that contained our main() method. Inside that same project, we had another class called RectangleComponent that had a different source code. How were the contents of the RectangleComponent class read if it did not contain a main() method? If you go back to our FrameViewer’s source code, you will notice that the RectangleComponent class was indeed addressed. Whenever you see this line (see the picture), it means that an instance of a certain class has been created. We refer to this instance of a class as an object. The constructor is used to create an object of a class. It may receive parameters that will define certain characteristics of that object. This one receives no parameters.
What are methods? You have already seen the main() method that at least one of our classes must contain in order to get the program running. However, there are many other methods that may be addressed once they have been defined. Simply speaking, methods are things that an object can do. Going back to our RectangleComponent class, we have created an object of the class Rectangle and called it box. Then, we used the method translate in order to change our object’s x and y coordinates. As you can see in the picture, there are many other methods that we can call.
Another example Suppose we want to sew a dress. Chances are we are going to have to follow a pattern. That pattern contains all of the information that we need in order to know how we have to create the dress. This pattern, in Java, would be the class. Each dress that we create from this pattern will be a real dress and will have its own characteristics, such as a (different) color, length or sleeves. Each particular creation is what we would call an object in Java.
Another example (continued) Let us assume there is an existent Dress class (you will be learning how to implement your very own classes later on). Let us also assume that it has an existing constructor where you can specify the real dress’ color, length (short, medium or long) and how many sleeves it has (0, 1, or 2). From one same pattern (class), we have created two independent dresses (objects).
Another example (continued) Now, after taking a closer look at our dress, we notice that our second dress, d2, it is not what actually we wanted. How about if we add one sleeve to it?
What is the Java API? The Java API is a documentation that contains the set of classes that is included with the Java Development Environment. It provides descriptions on each existing class, its constructors, and methods. Without a doubt, it is a very handy tool for Java developers. To find the Java API, simply search for “Java API” in your favorite search engine. As a quick example on how to browse the Java API, let’s look for the Rectangle class that we used in Lab 1.
Java API (continued) You will first encounter a description of the class you are browsing.
Java API (continued) The “field” and “constructor” summaries will provide everything you need to know to create an object of the class you are browsing.
Java API (continued) Finally, the “method” summary contains a list of all the methods that objects of the class can call.