1 / 11

Classes, Objects , Methods, and the Java API

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?.

angie
Download Presentation

Classes, Objects , Methods, and the Java API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes, Objects, Methods, and the Java API ICOM4015, Fall 2014 A brief introduction (to be studied before Lab 2) Created By katyai. borgos Revised By Amir H. Chinaei

  2. 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.

  3. 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.

  4. 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, methodsare 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.

  5. 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 objectin Java.

  6. 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).

  7. 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?

  8. 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.

  9. Java API (continued) You will first encounter a description of the class you are browsing.

  10. 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.

  11. Java API (continued) Finally, the “method” summary contains a list of all the methods that objects of the class can call.

More Related