A Secure and Reliable Document Management System is Essential.docx
Object Oriented Programming in Android Studio
1. 1. Start a newandroidproject
2. SelectEmptyactivity
3. Setthe name andpackage name andlocation
Package name includesDomainname andcompanyname anddepartmentetc.
4. In Projectpanel rightclickonapp folderthenselectnew module
3. 6. Setthe libraryname andclassname
7. You can see the package folders
MainActivity.javaisin“oopapps”folder
Person.javaisin“mylib”folder
WhenyoubuildyourprojectAndroidStudiomakesafoldercalledbuildinmylibfolder
inside thatislibsfolderand mylib.jarisinside that.
You can use mylib.jarinotherprojectlike .dll classlibraryinvisual studio.
4.
5. Java Classes/Objects
Java isan object-orientedprogramminglanguage.
EverythinginJavaisassociatedwithclasses andobjects,alongwithitsattributesandmethods.For
example:inreal life,acar isan object.The car has attributes,suchasweightandcolor,and methods,
such as drive andbrake.
A Classislike anobjectconstructor,or a "blueprint"forcreating objects.
8. Insertsome code in MyPerson.class
Line 1 : Indicatesthe package containingthe Javaclass.
Line 3 : The publickeywordisan access modifier,meaningthatitisusedto setthe
access level forclasses.
We divide modifiersintotwogroups:
I. AccessModifiers - controlsthe accesslevel
II. Non-AccessModifiers - donotcontrol access level,butprovidesother
functionality
For classwe have 2 access modifier
I. Public: The classis accessible byanyotherclass
II. Default:The classis onlyaccessible by classesinthe same package.Thisis
usedwhenyoudon'tspecifyamodifier(like onlyclassPerson).
Line 5, 6, 7 : we create some variable.
I. These variablesare private thatmeanstheyare accessible onlyinthisclass
II. Whenyoucreate a variable like “intnumber;”numberiszero but when you
create like this“Integernumber;”numberisnull.
6. Java Variables
Variablesare containersforstoringdatavalues.
In Java,there are differenttypesof variables,forexample:
String - storestext,suchas "Hello".Stringvaluesare surroundedbydouble quotes
int - storesintegers(whole numbers),withoutdecimals,suchas123 or -123
float - storesfloatingpointnumbers,withdecimals,suchas19.99 or -19.99
char - storessingle characters,suchas'a' or 'B'. Char valuesare surroundedbysingle quotes
boolean- storesvalueswithtwostates:true or false
9. Inside PersonclassblockrightclickandselectGenerate (Alt+Insert)
10. SelectConstructor
11. PressSelectNone button.
7. Java Constructors
A constructorin Javais a special methodthatisusedto initializeobjects.The constructoriscalledwhen
an objectof a classiscreated.It can be usedtoset initial valuesforobjectattributes.
Note that the constructorname must match the classname,and itcannot have a return type (like void).
Alsonote that the constructoris calledwhenthe objectiscreated.
All classeshave constructorsbydefault:if youdonot create a classconstructor yourself,Javacreates
one for you.However,thenyouare notable to setinitial valuesforobjectattributes.
Constructorscan alsotake parameters,whichisusedtoinitializeattributes.
8. 12. Insertthiscode to Personclassconstructor:
13. In MainActivity.javafile,inOnCreate method type “MyPerson”thenpress(Alt+Enter)
14. SelectAdddependencyonmodule ‘mylib’thisjobaddsanotherbuild.gradle andmake all
dependencytoyourmodule.
9. 15. In MainActivity.javafile,inOnCreate method rightclickon “MyPerson”then select“Show
ContextActions”
16. Select“ImportClass”
17. Insertcode like picture andrunproject
Firstline createsmyPersonobjectfromMyPersonclass.
Whenwe create an objectfrom a class,the constructor of the class isexecuted.
Whenwe type “myPerson.”AndroidStudio offertoussome propertyandmethodbut
inthiscase we cannot see firstName orlastName andnationalIDbecause theyare
private.
10. As youcan see inthe picture,the logisnotwhat I expected.Whatisthe solution?
To answerthisquestion,we needto discussthe conceptof Encapsulation.
Encapsulation
The meaningof Encapsulation,istomake sure that "sensitive"dataishiddenfromusers.Toachieve
this,youmust:
declare classvariables/attributesasprivate
provide publicgetandsetmethodstoaccessand update the value of a private variable
Get and Set
We knowthatprivate variablescanonlybe accessedwithinthe same class(anoutside classhasno
access to it).However,itispossible toaccessthemif we provide publicgetandsetmethods.
The get methodreturnsthe variable value,andthe setmethodsetsthe value.
Syntax forboth isthat theystart witheithergetorset,followedbythe name of the variable,withthe
firstletterinuppercase.
Why Encapsulation?
Bettercontrol of class attributesandmethods
Classattributescanbe made read-only(if youonlyuse the getmethod),orwrite-only(ifyou
onlyuse the setmethod)
Flexible:the programmercanchange one part of the code withoutaffectingotherparts
Increasedsecurityof data
18. Go to MyPerson.classinsideclassblockpress(Alt+Enter),selectGetterandSetter
11. 19. Selectall 3 variables
20. Nowchange Log in MainActivity.java
As youcan see,the outputiscorrect. If your logshowsa lot of data you can searchby tag inthis
case is“info”.
In the followingarticles,Iwill discussotherissuesof the object-orientedprogramminginAndroid
Studio.
In thisarticle,Iusedthe resourcesof the www.w3schools.com.
I will be happytoknowyour commentsandquestions.
Sayed Mahmood Ghaemmaghami