SlideShare a Scribd company logo
More Java: Encapsulation, Getters, Setters,
Anonymous Class
1
CS300
Java Packages
 Provide a mechanism for grouping related
types together in a universally unique
namespace.
 java.lang
 Java.util
 How to name a package?
 Domain name reversed
 Ex. domain android.com will be package com.android
 Plus name of the program
 Ex. com.android.notepad
CS300
2
Access Modifiers
 Keywords that modify the visibility of the
declarations to which they are applied
 Private: most restrictive. Not visible outside the
block that contains it.
 Default or package access: next most restrictive.
Visible only from other classes in package.
 Protected: permits all default access rights plus
access from within any subtype.
 Public: allows access from anywhere.
CS300
3
Encapsulation
 The idea that an object should never reveal
details about itself that it does not intend to
support.
 Getters and Setters:
 Common example of encapsulation in Java
CS300
4
Getters and Setters example
 Create a class Contact that extends
Comparable<Contact>:
 Fields: name, age, email
 Create constructor with arguments: name, age,
email
 Create getters and setters for all three fields
 Create method compareTo to sort contacts by e-
mail
 Test
CS300
5
Anonymous Class
 Callback: your code needs to be notified
when something in the UI changes.
 Ex. a button is pushed and we need to change
state, new data has arrived from the network and
it needs to be displayed
 Java provides idiom to pass blocks in code
 Anonymous classes are a handy tool for
expressing many kinds of code blocks.
CS300
6
7
Without anonymous class With anonymous class
public class myDataModel{
//Callback class
private class keyHandler implements
View.onKeyListener {
public boolean onKey(View v,
int keyCode, KeyEvent event) {
handleKey(v, keyCode,
event);
}
}
/* @param view in the view we model */
public myDataModel(View view){
view.setOnKeyListener(new
KeyHandler())
}
/** Handle a key event **/
void handleKey(View v, int keyCode,
KeyEvent event){
// key handling code goes here …
}
}
public class myDataModel{
/* @param view in the view we model */
public myDataModel(View view) {
view.setOnKeyListener(
// this is an anonymous class!!
new View.OnKeyListener() {
public boolean onKey(View v, int
keyCode, KeyEvent event) {
handleKey(v, keyCode,
event);
}
} );
/** Handle a key event **/
void handleKey(View v, int keyCode,
KeyEvent event){
// key handling code goes here …
}
}
CS300
References
 Programming Android by Zigurd Mednieks,
Laird Dornin, G. Blake Meike, Masumi
Nakamura
CS300
8
Install ADT and AVD
 http://developer.android.com/sdk/index.html#d
ownload
 If you already have eclipse use an Existing IDE
option
CS300
9
Install notes
 Don’t forget to configure the ADT (Eclipse,
Window, Preferences, Android)
 Do not forget to set an AVD target (run
configuration)
 Do not forget to assign SD memory
 Check devices: use the proper one
CS300
10
Hello Android!!
 New android project
 Delete activity_main.xml
 Right click layout -> create new xml. Choose
relative layout
 Configure resolution: samsung nexus 4 inch
screen, 480-by-800
 Create AVD
CS300
11
Hello Android!!
 Drawables:
 Hdpi: high density
 Mdpi: medium density
 Ldpi: low density
 Xhdpi: extra high
 Change ID property for relative layout
 +: create new variable with name
 Change BG property
 RGB color map
CS300
12
Hello Android!!
 Add a TextView
 Configure text property: create a new string
resource (good practice)
 R.String: name
 String: content
 Configure text size:
 Dp: density independent pixel
 Configure layout margin top
 Text color: #00F
 Text Style: bold
CS300
13
Hello Android!!
 Add image view
 Create new drawable: be careful with names! No
hyphens allowed
 Change:
 Id
 Layout below: put it under your textview
 Layout center horizontal
 Src: do not forget to add your image in a drawable
directory
CS300
14

More Related Content

DOCX
Object Oriented Programming in Android Studio
DOCX
Bt0082 visual basic
PPTX
Django and working with large database tables
PPT
Parameters
PPT
Ch02 primitive-data-definite-loops
PPT
conditional statements
PPT
Ch01 basic-java-programs
PPTX
Constructor and destructor in c++
Object Oriented Programming in Android Studio
Bt0082 visual basic
Django and working with large database tables
Parameters
Ch02 primitive-data-definite-loops
conditional statements
Ch01 basic-java-programs
Constructor and destructor in c++

Similar to Encapsulation getters setters_anonymousclass (20)

PPT
java-corporate-training-institute-in-mumbai
PPT
java-corporate-training-institute-in-mumbai
PPT
java_ notes_for__________basic_level.ppt
PPT
java ppt for basic intro about java and its
PPT
Java - A parent language and powerdul for mobile apps.
PPT
Java intro
PPT
Java01
PPT
Java01
PPT
Present the syntax of Java Introduce the Java
PPTX
mukul Dubey.pptx
PPT
java01.ppt
PPT
java01.ppt
PPT
java01.ppt
PPT
OOPs concept and java Environment decsion making statement looping array and ...
PPT
java01.ppt
PPT
java01.ppt
PPT
java01.ppt
PPT
java01.ppt
PPT
java01.ppt
java-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbai
java_ notes_for__________basic_level.ppt
java ppt for basic intro about java and its
Java - A parent language and powerdul for mobile apps.
Java intro
Java01
Java01
Present the syntax of Java Introduce the Java
mukul Dubey.pptx
java01.ppt
java01.ppt
java01.ppt
OOPs concept and java Environment decsion making statement looping array and ...
java01.ppt
java01.ppt
java01.ppt
java01.ppt
java01.ppt
Ad

More from Hoang Nguyen (20)

PPTX
Rest api to integrate with your site
PPTX
How to build a rest api
PPTX
Api crash
PPTX
Smm and caching
PPTX
Optimizing shared caches in chip multiprocessors
PPTX
How analysis services caching works
PPTX
Hardware managed cache
PPTX
Directory based cache coherence
PPTX
Cache recap
PPTX
Python your new best friend
PPTX
Python language data types
PPTX
Python basics
PPTX
Programming for engineers in python
PPTX
Learning python
PPTX
Extending burp with python
PPTX
Cobol, lisp, and python
PPT
Object oriented programming using c++
PPTX
Object oriented analysis
PPTX
Object model
PPTX
Data structures and algorithms
Rest api to integrate with your site
How to build a rest api
Api crash
Smm and caching
Optimizing shared caches in chip multiprocessors
How analysis services caching works
Hardware managed cache
Directory based cache coherence
Cache recap
Python your new best friend
Python language data types
Python basics
Programming for engineers in python
Learning python
Extending burp with python
Cobol, lisp, and python
Object oriented programming using c++
Object oriented analysis
Object model
Data structures and algorithms
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Tartificialntelligence_presentation.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
1. Introduction to Computer Programming.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Getting Started with Data Integration: FME Form 101
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Tartificialntelligence_presentation.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25-Week II
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
1. Introduction to Computer Programming.pptx
A Presentation on Artificial Intelligence
Getting Started with Data Integration: FME Form 101
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology

Encapsulation getters setters_anonymousclass

  • 1. More Java: Encapsulation, Getters, Setters, Anonymous Class 1 CS300
  • 2. Java Packages  Provide a mechanism for grouping related types together in a universally unique namespace.  java.lang  Java.util  How to name a package?  Domain name reversed  Ex. domain android.com will be package com.android  Plus name of the program  Ex. com.android.notepad CS300 2
  • 3. Access Modifiers  Keywords that modify the visibility of the declarations to which they are applied  Private: most restrictive. Not visible outside the block that contains it.  Default or package access: next most restrictive. Visible only from other classes in package.  Protected: permits all default access rights plus access from within any subtype.  Public: allows access from anywhere. CS300 3
  • 4. Encapsulation  The idea that an object should never reveal details about itself that it does not intend to support.  Getters and Setters:  Common example of encapsulation in Java CS300 4
  • 5. Getters and Setters example  Create a class Contact that extends Comparable<Contact>:  Fields: name, age, email  Create constructor with arguments: name, age, email  Create getters and setters for all three fields  Create method compareTo to sort contacts by e- mail  Test CS300 5
  • 6. Anonymous Class  Callback: your code needs to be notified when something in the UI changes.  Ex. a button is pushed and we need to change state, new data has arrived from the network and it needs to be displayed  Java provides idiom to pass blocks in code  Anonymous classes are a handy tool for expressing many kinds of code blocks. CS300 6
  • 7. 7 Without anonymous class With anonymous class public class myDataModel{ //Callback class private class keyHandler implements View.onKeyListener { public boolean onKey(View v, int keyCode, KeyEvent event) { handleKey(v, keyCode, event); } } /* @param view in the view we model */ public myDataModel(View view){ view.setOnKeyListener(new KeyHandler()) } /** Handle a key event **/ void handleKey(View v, int keyCode, KeyEvent event){ // key handling code goes here … } } public class myDataModel{ /* @param view in the view we model */ public myDataModel(View view) { view.setOnKeyListener( // this is an anonymous class!! new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { handleKey(v, keyCode, event); } } ); /** Handle a key event **/ void handleKey(View v, int keyCode, KeyEvent event){ // key handling code goes here … } } CS300
  • 8. References  Programming Android by Zigurd Mednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura CS300 8
  • 9. Install ADT and AVD  http://developer.android.com/sdk/index.html#d ownload  If you already have eclipse use an Existing IDE option CS300 9
  • 10. Install notes  Don’t forget to configure the ADT (Eclipse, Window, Preferences, Android)  Do not forget to set an AVD target (run configuration)  Do not forget to assign SD memory  Check devices: use the proper one CS300 10
  • 11. Hello Android!!  New android project  Delete activity_main.xml  Right click layout -> create new xml. Choose relative layout  Configure resolution: samsung nexus 4 inch screen, 480-by-800  Create AVD CS300 11
  • 12. Hello Android!!  Drawables:  Hdpi: high density  Mdpi: medium density  Ldpi: low density  Xhdpi: extra high  Change ID property for relative layout  +: create new variable with name  Change BG property  RGB color map CS300 12
  • 13. Hello Android!!  Add a TextView  Configure text property: create a new string resource (good practice)  R.String: name  String: content  Configure text size:  Dp: density independent pixel  Configure layout margin top  Text color: #00F  Text Style: bold CS300 13
  • 14. Hello Android!!  Add image view  Create new drawable: be careful with names! No hyphens allowed  Change:  Id  Layout below: put it under your textview  Layout center horizontal  Src: do not forget to add your image in a drawable directory CS300 14

Editor's Notes

  • #4: Example of accessibility and packages