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
Inner classes ,annoumous and outer classes in java
PDF
Classes and Nested Classes in Java
PPT
Evaluación primaria incluyendo lomce
PPT
Memory caching
PDF
Ldap
Object Oriented Programming in Android Studio
Bt0082 visual basic
Django and working with large database tables
Inner classes ,annoumous and outer classes in java
Classes and Nested Classes in Java
Evaluación primaria incluyendo lomce
Memory caching
Ldap

Viewers also liked (20)

PDF
Problem Statement Lumos
PPTX
DOC
2016 RESUME ISCAHM
PPTX
T4 Mov Obrer
PDF
LIMITE DE FUNCIONES INDETERMINADAS
PPTX
Week 6 final project (1)
PPTX
05 uni 11352 parte 3
PDF
Preparing Codes for Intel Knights Landing (KNL)
PPTX
Object model
PPT
Web 2.0 Tools in Your Classroom
PPT
AR+S The Role Of Abstraction In Human Computer Interaction
PPTX
Interface and abstraction
PDF
IEEE DEST CEE 2013 - Paper 4 (1) (1)
PDF
Java topology abstraction service for ip-vp ns
PDF
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
DOC
Core java questions
PPT
File class.48
PDF
PDF
Intro to JavaScript
PPTX
IoT and META-engineering with Service Templates
Problem Statement Lumos
2016 RESUME ISCAHM
T4 Mov Obrer
LIMITE DE FUNCIONES INDETERMINADAS
Week 6 final project (1)
05 uni 11352 parte 3
Preparing Codes for Intel Knights Landing (KNL)
Object model
Web 2.0 Tools in Your Classroom
AR+S The Role Of Abstraction In Human Computer Interaction
Interface and abstraction
IEEE DEST CEE 2013 - Paper 4 (1) (1)
Java topology abstraction service for ip-vp ns
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Core java questions
File class.48
Intro to JavaScript
IoT and META-engineering with Service Templates
Ad

Similar to Encapsulation anonymous class (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 Harry Potter (20)

PDF
How to build a rest api.pptx
PPTX
Business analytics and data mining
PPTX
Big picture of data mining
PPTX
Data mining and knowledge discovery
PPTX
Cache recap
PPTX
Directory based cache coherence
PPTX
How analysis services caching works
PPTX
Optimizing shared caches in chip multiprocessors
PPTX
Hardware managed cache
PPTX
Smm & caching
PPTX
Data structures and algorithms
PPT
Abstract data types
PPTX
Abstraction file
PPTX
Object model
PPTX
Concurrency with java
PPT
Abstract class
PPTX
Object oriented analysis
PPTX
Api crash
PPTX
Rest api to integrate with your site
PPTX
Inheritance
How to build a rest api.pptx
Business analytics and data mining
Big picture of data mining
Data mining and knowledge discovery
Cache recap
Directory based cache coherence
How analysis services caching works
Optimizing shared caches in chip multiprocessors
Hardware managed cache
Smm & caching
Data structures and algorithms
Abstract data types
Abstraction file
Object model
Concurrency with java
Abstract class
Object oriented analysis
Api crash
Rest api to integrate with your site
Inheritance

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MIND Revenue Release Quarter 2 2025 Press Release
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation theory and applications.pdf

Encapsulation anonymous class

  • 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