Android Studio Java Object Oriented
In this article, we will get acquainted with the concepts of the object oriented in Android Studio step by step. The programming language I work with is Java. Section 1.
Welcome to first section
1. Start a new android project
2. Select Empty activity
3. Set the name and package name and location
Package name includes Domain name and company name and department etc.
4. In Project panel right cpck on app folder then select new module
5. Select Java or Kotlin Library
6. Set the library name and class name
7. You can see the package folders
-
MainActivity.java is in “oopapps” folder
-
Person.java is in “mylib” folder
-
When you build your project Android Studio makes a folder called build in mylib folder inside that is libs folder and mylib.jar is inside that.
-
You can use mylib.jar in other project like .dll class library in visual studio.
Java Classes/Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.
8. Insert some code in MyPerson.class
- Line 1 : Indicates the package containing the Java class.
- Line 3 : The public keyword is an access modifier, meaning that it is used to set the access level for classes.
We divide modifiers into two groups:
- 1. Access Modifiers - controls the access level
- 2. Non-Access Modifiers - do not control access level, but provides other functionality
For class we have 2 access modifier
- 1. Public : The class is accessible by any other class
- 2. Default : The class is only accessible by classes in the same package. This is used when you don't specify a modifier(like only class Person).
Line 5, 6, 7 : we create some variable.
- 1. These variables are private that means they are accessible only in this class
- 2. When you create a variable like “int number;” number is zero but when you create like this “Integer number;” number is null.
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
- String stores text, such as "Hello". String values are surrounded by double quotes
- int stores integers (whole numbers), without decimals, such as 123 or -123
- float stores floating point numbers, with decimals, such as 19.99 or -19.99
- char stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
- boolean stores values with two states: true or false
9. Inside Person class block right click and select Generate (Alt+Insert)
10. Select Constructor
11. Press Select None button.
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
Note that the constructor name must match the class name, and it cannot have a return type (like void).
Also note that the constructor is called when the object is created.
All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.
Constructors can also take parameters, which is used to initialize attributes.
12. Insert this code to Person class constructor:
13. In MainActivity.java file, in On Create method type “MyPerson” then press (Alt+Enter)
14. Select Add dependency on module ‘mylib’ this job adds another build.gradle and make all dependency to your module.
15. In MainActivity.java file, in On Create method right click on “MyPerson” then select “Show Context Actions”
16. Select “Import Class”
17. Insert code like picture and run project
-
First line creates myPerson object from MyPerson class.
-
When we create an object from a class, the constructor of the class is executed.
-
When we type “myPerson.” Android Studio offer to us some property and method but in this case we cannot see firstName or lastName and nationalID because they are private.
-
As you can see in the picture, the log is not what I expected. What is the solution?
-
To answer this question, we need to discuss the concept of Encapsulation.
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable
For class we have 2 access modifier
Get and Set
We know that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case.
Why Encapsulation?
- Better control of class attributes and methods
- Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
- Flexible: the programmer can change one part of the code without affecting other parts
- Increased security of data
18. Go to MyPerson.class inside class block press (Alt+Enter), select Getter and Setter
19. Select all 3 variables
20. Now change Log in MainActivity.java
As you can see, the output is correct. If your log shows a lot of data you can search by tag in this case is “info”.
In the following articles, I will discuss other issues of the object-oriented programming in Android Studio.
In this article, I used the resources of the
www.w3schools.com
and
www.javacup.ir
Special thanks to Mr.
Sadegh Ali Akbari
for teaching me useful materials with his educational videos.
I will be happy to know your comments and questions.