Download presentation
Presentation is loading. Please wait.
Published byDwight Barton Modified over 9 years ago
1
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
2
Exam returned in lab sessions Average: 43/65 HW6 due March 16 PA4 due March 16 Announcement
3
Chapter 8 Class Constructors Data fields UML notation Objectives
4
Class
5
Suppose you want to develop a graphical user interface as shown below. How do you program it? Motivation
6
object An entity in the real world that can be distinctly identified. A student, a desk, a circle, a button, and even a loan can all be viewed as objects. has a unique identity, state, and behaviors. State = a set of data fields (also known as properties) with their current values. Behavior = a set of methods Object
7
Classes are constructs that define objects of the same type Class vs. Object
8
Class Example
9
UML
10
UML class diagram UML Notation
11
Constructor
12
Constructors are a special kind of methods that are invoked to construct objects. Constructor Circle(double newRadius) { radius = newRadius; } Constructor with no argument Constructor with argument Circle() { }
13
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. Constructor
14
Creating objects using constructors Constructor new ClassName(); new Circle(); new Circle(5.0);
15
A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. Constructor
16
Declaring & creating objects in a single step Two steps Creating Objects Circle myCircle = new Circle(); declare create //step 1: declare Circle myCircle; //step 2: create myCircle = new Circle();
17
Accessing Data Fields & Methods
18
Referencing the object’s data fields: Invoking the object’s method: Access myCircle.radius myCircle.getArea()
19
The data fields can be of reference types Example: name data field is of type String Data Fields public class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // has default value false char gender; // c has default value '\u0000‘ //methods … }
20
If a data field of a reference type does not reference any object, the data field holds a special literal value: null. null
21
The default value of a data field is null for a reference type 0 for a numeric type false for a boolean type '\u0000' for a char type However, Java assigns no default value to a local variable inside a method. Default Values
22
Java assigns no default value to a local variable inside a method. Default Values public class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } Compilation error: variables not initialized
23
Accessing methods and data fields Program TestCircle1 Run TestTV Run TV
24
Primitive type copy Copying
25
Reference type copy Copying
26
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 referenced. This object is known as garbage. Garbage is automatically collected by JVM. Garbage Collector
27
TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable. Garbage Collector
28
Chapter 8 OOP Constructors Previously
29
Instance fields belong to a specific instance. Instance methods are invoked by an instance of the class. i.e. non-static methods Instance Fields
30
Static methods are not tied to a specific object. Static variables are shared by all the instances of the class. Static constants are final variables shared by all the instances of the class. All declared using static keyword Static
31
Example Static vs. Instance
32
Static fields or methods can be used from instance or static methods. Instance fields or methods can be only used from instance methods. So: a variable or method that does not depend on a specific instance of the class, should be specified as static. Static vs. Instance
33
public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { int j= i; // Wrong because i is an instance variable m1(); // Wrong because m1() is an instance method } public void m1() { i = i + k + m2(i, k); // Correct: since instance and static variables and //methods can be used in an instance method } public static int m2( int i, int j) { return ( int )(Math.pow(i, j)); }
34
Static vs. Instance public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { Foo foo = new Foo(); int j= foo.i; // OK foo.m1(); // OK } public void m1() { i = i + k + m2(i, k); } public static int m2( int i, int j) { return ( int )(Math.pow(i, j)); }
35
Which one is a better design? Static vs. Instance
36
This example adds a class (i.e. static) variable numberOfObjects to track the number of Circle objects created. Program TestCircle2Run Circle2
37
1. Package access (default in Java) The class, variable, or method can be accessed by any class in the same package. 2. public The class, data, or method is visible to any class in any package. 3. private The data or methods can be accessed only by the declaring class. Accessibility
38
Access C1 methods & fields from C2/C3? Accessibility Example public class C1 { public int x; int y; private int z; public void m1() {..} void m2() {..} private void m3() {..} } Package 1 public class C2 { void aMethod() { C1 o = new C1(); } public class C3 { void aMethod() { C1 o = new C1(); } Package 2
39
Accessibility Example public class C1 { public int x; int y; private int z; public void m1() {..} void m2() {..} private void m3() {..} } Package 1 public class C2 { void aMethod() { C1 o = new C1(); //can access o.x; //can access o.y; //cannot access //o.z; //can invoke //o.m1(); //can invoke o.m2() //cannot invoke //o.m3(); } public class C3 { void aMethod() { C1 o = new C1(); //can access o.x; //cannot access //o.y; //cannot access //o.z; //can invoke //o.m1(); //cannot invoke //o.m2(); //cannot invoke //o.m3(); } Package 2
40
Which classes can access C1? Accessibility Example class C1 { … } Package 1 public class C2 { //can access C1? } public class C3 { //can access C1? //can access C2? } Package 2
41
Which classes can access C1? Accessibility Example class C1 { … } Package 1 public class C2 { //can access C1 } public class C3 { //cannot access C1 //can access C2 } Package 2
42
The private modifier restricts access to within a class The default modifier restricts access to within a package The public modifier enables unrestricted access. Accessibility Summary
43
Example: Accessibility public class Foo { private boolean x; public void test() { System.out.println(x); System.out.println(convert()); } private int convert( boolean b) { return x ? 1 : -1; } OK
44
Example: Because x and convert are private members Accessibility public class Test { public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); System.out.println(foo.convert(foo.x)); } Error!
45
To protect data. To make class easy to maintain. Why Private?
46
The get and set methods are used to read and modify private properties. Data encapsulation get/set The - sign indicates private modifier
47
Data encapsulation Program Circle3 Run TestCircle3
48
Passing by value for primitive type value (the value is passed to the parameter) Passing by value for reference type value (the value is the reference to the object) Program TestPassObject Run
49
GUI
50
PA4 and HW6 extended to Sunday 4:00 pm Test data available for PA4 (on Sakai) Announcements
51
When you develop programs to create graphical user interfaces, you will use Java classes such as JFrame, JButton, and JList to create frames, buttons, lists, and so on. Here is an example that creates two windows using the JFrame class. GUI Program TestFrame Run
52
You can add graphical user interface components, such as buttons, labels, text fields, combo boxes, lists, and menus, to the window. The components are defined using classes. Here is an example to create buttons, labels, text fields, check boxes, radio buttons, and combo boxes. GUI Program GUIComponents Run
53
Array of Objects
54
An array of objects is actually an array of reference variables. So invoking circleArray[1].getArea() involves two levels of referencing. circleArray references to the entire array. circleArray[1] references to a Circle object. Array of Objects Circle[] circleArray = new Circle[10];
55
Array of Objects Circle[] circleArray = new Circle[10];
56
Summarizing the areas of the circles Program 56 TotalAreaRun
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.