1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are...
-
Upload
jason-norton -
Category
Documents
-
view
215 -
download
3
Transcript of 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are...
![Page 1: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/1.jpg)
1
Chapter 8 Objects and Classes
![Page 2: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/2.jpg)
2
MotivationsAfter learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java features are not sufficient for developing graphical user interfaces and large scale software systems. Suppose you want to develop a graphical user interface as shown below. How do you program it?
![Page 3: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/3.jpg)
Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:
Polymorphism Inheritance Encapsulation Abstraction Classes Objects Instance Method Message Parsing
3
![Page 4: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/4.jpg)
4
![Page 5: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/5.jpg)
5
OO Programming ConceptsObject-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods.
![Page 6: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/6.jpg)
OO Programming Concepts
Object - Objects have states and behaviors. Example: A dog has states - color, name, as well as behaviors - barking, eating. An object is an instance of a class.
6
![Page 7: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/7.jpg)
7
Objects
An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.
Class Name: Circle Data Fields:
radius is _______ Methods:
getArea
Circle Object 1 Data Fields:
radius is 10
Circle Object 2 Data Fields:
radius is 25
Circle Object 3 Data Fields:
radius is 125
A class template
Three objects of the Circle class
![Page 8: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/8.jpg)
8
Classes
Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.
![Page 9: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/9.jpg)
Classes
Class - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
9
![Page 10: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/10.jpg)
Classes Local variables: Variables defined inside methods,
constructors are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method or constructor of that particular class.
10
![Page 11: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/11.jpg)
Access Control Modifiers:
Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:
Visible to the package, the (default). No modifiers are needed.
Visible to the class only (private). Visible to the world (public). Visible to the package and all subclasses (protected).
11
![Page 12: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/12.jpg)
12
Classes class Circle {
/** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; }
}
Data field
Method
Constructors
![Page 13: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/13.jpg)
13
UML Class Diagram
Circle
radius: double Circle()
Circle(newRadius: double)
getArea(): double
circle1: Circle radius = 1.0
Class name
Data fields
Constructors and methods
circle2: Circle radius = 25
circle3: Circle radius = 125
UML Class Diagram
UML notation for objects
![Page 14: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/14.jpg)
Package & Import
Java Package: In simple, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier.
Import statements: In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.
14
![Page 15: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/15.jpg)
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The 'new' key word is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Example : Circle1 myCircle = new Circle1(5.0);
15
![Page 16: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/16.jpg)
16
Example: Defining Classes and Creating Objects
(Circle1) Objective: Demonstrate creating objects,
accessing data, and using methods.
![Page 17: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/17.jpg)
17
![Page 18: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/18.jpg)
18
![Page 19: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/19.jpg)
19
Example: Defining Classes and Creating Objects (TV)
Objective: Demonstrate creating objects, accessing data, and using methods.
![Page 20: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/20.jpg)
20
![Page 21: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/21.jpg)
21
![Page 22: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/22.jpg)
Test TV
22
![Page 23: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/23.jpg)
Constructors
When discussing about classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler builds a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.
23
![Page 24: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/24.jpg)
24
Constructors
Circle() {}
Circle(double newRadius) { radius = newRadius;}
Constructors are a special kind of methods that are invoked to construct objects.
![Page 25: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/25.jpg)
25
Constructors, cont.A constructor with no parameters is referred to as a no-arg constructor.
· Constructors must have the same name as the class itself.
· Constructors do not have a return type—not even void. (Error : public void Circle() {})
· Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
![Page 26: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/26.jpg)
26
Creating Objects Using Constructors
new ClassName();
Example:new Circle(); (Using First Constructor)
new Circle(5.0); (Using Second Constructor)
![Page 27: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/27.jpg)
27
Default Constructor
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.
![Page 28: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/28.jpg)
28
Declaring Object Reference Variables
To reference an object, assign the object to a reference variable.
To declare a reference variable, use the syntax:
ClassName objectRefVar;
Example:Circle myCircle;
![Page 29: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/29.jpg)
29
Declaring/Creating Objectsin a Single Step
ClassName objectRefVar = new ClassName();
Example:Circle myCircle = new Circle();
Create an objectAssign object reference
![Page 30: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/30.jpg)
30
Accessing Objects
(Object member access operator or “ . ”) Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius (instasnce variable)
Invoking the object’s method:
objectRefVar.methodName(arguments)
e.g., myCircle.getArea() (instasnce method)
![Page 31: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/31.jpg)
31
Trace Code
Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100;
Declare myCircle
no valuemyCircle
animation
![Page 32: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/32.jpg)
32
Trace Code, cont.
Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle
radius: 5.0
no valuemyCircle
Create a circle
animation
![Page 33: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/33.jpg)
33
Trace Code, cont.
Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle
radius: 5.0
reference valuemyCircle
Assign object reference to myCircle
animation
![Page 34: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/34.jpg)
34
Trace Code, cont.Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle
radius: 5.0
reference valuemyCircle
no valueyourCircle
Declare yourCircle
animation
![Page 35: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/35.jpg)
35
Trace Code, cont.Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle
radius: 5.0
reference valuemyCircle
no valueyourCircle
: Circle radius: 0.0
Create a new Circle object
animation
![Page 36: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/36.jpg)
36
Trace Code, cont.Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle
radius: 5.0
reference valuemyCircle
reference valueyourCircle
: Circle radius: 1.0
Assign object reference to yourCircle
animation
![Page 37: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/37.jpg)
37
Trace Code, cont.Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100; : Circle radius: 5.0
reference valuemyCircle
reference valueyourCircle
: Circle radius: 100.0
Change radius in
yourCircle
animation
![Page 38: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/38.jpg)
38
CautionRecall that you use
Math.methodName(arguments) (e.g., Math.pow(3, 2.5))
to invoke a method in the Math class. Can you invoke getArea() using Circle1.getArea()? The answer is no. All the methods used before this chapter are static methods, which are defined using the static keyword. However, getArea() is non-static. It must be invoked from an object using
objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).
More explanations will be given in the section on “Static Variables, Constants, and Methods.”
![Page 39: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/39.jpg)
39
Reference Data FieldsThe data fields can be of reference types. For example, the following Student class contains a data field name of the String type.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // c has default value '\u0000'
}
![Page 40: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/40.jpg)
40
The null Value
If a data field of a reference type does not reference any object, the data field holds a special literal value, null.
![Page 41: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/41.jpg)
41
Default Value for a Data FieldThe default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and '\u0000' for a char type. However, Java assigns no default value to a local variable inside a method. public class Test {
public static void main(String[] args) {
Student student = new Student();
System.out.println("name? " + student.name);
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
}
}
![Page 42: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/42.jpg)
42
Example
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
Java assigns no default value to a local variable inside a method.
![Page 43: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/43.jpg)
43
Differences between Variables of Primitive Data Types and Object Types
1 Primitive type int i = 1 i
Object type Circle c c reference
Created using new Circle()
c: Circle
radius = 1
![Page 44: 1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,](https://reader030.fdocuments.in/reader030/viewer/2022032600/56649d965503460f94a7ed63/html5/thumbnails/44.jpg)
44
Copying Variables of Primitive Data Types and Object Types
i
Primitive type assignment i = j
Before:
1
j
2
i
After:
2
j
2
c1
Object type assignment c1 = c2
Before:
c2
c1
After:
c2
c1: Circle
radius = 5
C2: Circle
radius = 9
c1: Circle
radius = 5
C2: Circle
radius = 9