SlideShare a Scribd company logo
1 of 14
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

Similar to Encapsulation anonymous class

Application package
Application packageApplication package
Application package
JAYAARC
 
DTS s03e02 Handling the code
DTS s03e02 Handling the codeDTS s03e02 Handling the code
DTS s03e02 Handling the code
Tuenti
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
Abed Bukhari
 

Similar to Encapsulation anonymous class (20)

Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
Application package
Application packageApplication package
Application package
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
DTS s03e02 Handling the code
DTS s03e02 Handling the codeDTS s03e02 Handling the code
DTS s03e02 Handling the code
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
CS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptxCS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptx
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Dependency Injection for Android
Dependency Injection for AndroidDependency Injection for Android
Dependency Injection for Android
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
 
Introduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinIntroduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon Gauvin
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 

More from Luis Goldster

More from Luis Goldster (20)

Ruby on rails evaluation
Ruby on rails evaluationRuby on rails evaluation
Ruby on rails evaluation
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Lisp and scheme i
Lisp and scheme iLisp and scheme i
Lisp and scheme i
 
Ado.net &amp; data persistence frameworks
Ado.net &amp; data persistence frameworksAdo.net &amp; data persistence frameworks
Ado.net &amp; data persistence frameworks
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data mining
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
 
Cache recap
Cache recapCache recap
Cache recap
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Api crash
Api crashApi crash
Api crash
 
Object model
Object modelObject model
Object model
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Abstract class
Abstract classAbstract class
Abstract class
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

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

  1. Example of accessibility and packages