AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
-
Upload
marsha-wade -
Category
Documents
-
view
217 -
download
4
Transcript of AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
![Page 1: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/1.jpg)
1 AP Computer Science A – Healdsburg High School
Unit 9- Why Use Classes
- Anatomy of a Class
![Page 2: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/2.jpg)
2 AP Computer Science A – Healdsburg High School
Encapsulation
• A class (in OOP) encapsulates (groups together) data and actions relating to an object.
• Encapsulation hides the implementation details of the class from the users (clients) of the class.
![Page 3: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/3.jpg)
3 AP Computer Science A – Healdsburg High School
Encapsulation (cont’d) public class MyClass {
// Private fields: private <sometype> myField; ...
// Constructors: public MyClass (...) { ... } ... // Public methods: public <sometype> myMethod (...) { ... } ... // Private methods: private <sometype> myMethod (...) { ... } ... }
Public interface: public constructors and methods
![Page 4: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/4.jpg)
4 AP Computer Science A – Healdsburg High School
1. Fields (or instance variables)
• Fields describe the data of an object (think nouns!!).
• Fields are generally labeled private and therefore hidden from the client (only accessible from within the class).
![Page 5: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/5.jpg)
5 AP Computer Science A – Healdsburg High School
2. Constructors
• Constructors describe ways to create an object of a class and initialize the fields.
• Constructors are always labeled public and have the same name as the class.
• There may be more than one constructor for a class. If more than one, each must have different arguments (or parameters).
![Page 6: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/6.jpg)
6 AP Computer Science A – Healdsburg High School
3. Public Methods
• Methods describe the actions or questions that can be asked about an object (think verbs!!).
• Methods are generally labeled public. private (helper) methods can only be used within the class.
![Page 7: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/7.jpg)
7 AP Computer Science A – Healdsburg High School
3. Public Methods (cont.)
• In general, 3 classifications of methods:1. Accessor – “gets” information about the object.
2. Modifier – “sets” information about the object
(or changes the state of the object).
3. boolean – asks a true or false question about the
object.
![Page 8: AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.](https://reader036.fdocuments.in/reader036/viewer/2022083009/5697bfbf1a28abf838ca362a/html5/thumbnails/8.jpg)
8 AP Computer Science A – Healdsburg High School
3. Public Methods (cont.)
• To define a method:– decide between public and private (usually public)– give it a name– specify the types of parameters and give them
names– specify the method’s return type or choose void– write the method’s code
public [or private] returnType methodName (type1 name1, ..., typeN nameN){ ...}
HeaderBody