1 / 18

Introduction to Java Programming, 4E

Introduction to Java Programming, 4E. Y. Daniel Liang. Chapter 6 Objects and Classes. OO Programming Concepts Creating Objects and Object Reference Variables Differences between primitive data type and object type Automatic garbage collection Constructors

donat
Download Presentation

Introduction to Java Programming, 4E

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. Introduction toJava Programming, 4E Y. Daniel Liang

  2. Chapter 6 Objects and Classes • OO Programming Concepts • Creating Objects and Object Reference Variables • Differences between primitive data type and object type • Automatic garbage collection • Constructors • Modifiers (public, private and static) • Instance and Class Variables and Methods • Scope of Variables • Use the this Keyword • Case Studies (Mortgage class and Count class)

  3. OO Programming Concepts

  4. Class and Objects

  5. Class Declaration class Circle { double radius = 1.0; double findArea(){ return radius * radius * 3.14159; } }

  6. Declaring Object Reference Variables ClassName objectReference; Example: Circle myCircle;

  7. Creating Objects objectReference = new ClassName(); Example: myCircle = new Circle(); The object reference is assigned to the object reference variable.

  8. Declaring/Creating Objectsin a Single Step ClassName objectReference = new ClassName(); Example: Circle myCircle = new Circle();

  9. Differences between variables of primitive Data types and object types

  10. Copying Variables of Primitive Data Types and Object Types

  11. Garbage Collection As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer useful. This object is known as garbage. Garbage is automatically collected by JVM.

  12. Garbage Collection, cont TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The Java VM will automatically collect the space if the object is not referenced by any variable.

  13. Accessing Objects • Referencing the object’s data: objectReference.data myCircle.radius • Invoking the object’s method: objectReference.method myCircle.findArea()

  14. Example 6.1 Using Objects • Objective: Demonstrate creating objects, accessing data, and using methods.

  15. Constructors Circle(double r) { radius = r; } Circle() { radius = 1.0; } myCircle = new Circle(5.0); Constructors are a special kind of methods that are invoked to construct objects.

  16. Constructors, cont. A constructor with no parameters is referred to as a default constructor. ·Constructors must have the same name as the class itself. ·Constructors do not have a return type—not even void. ·Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

  17. Example 6.2 Using Classes from the Java Library • Objective: Demonstrate using classes from the Java library. Use the JFrame class in the javax.swing package to create two frames; use the methods in the JFrame class to set the title, size and location of the frames and to display the frames.

  18. Example 6.3 Using Constructors • Objective: Demonstrate the role of constructors and use them to create objects.

More Related