Learn the basic of Java programming for Android development.
REPLACE ALL IMAGES WITH MY OWN IMAGES LATER!!!!!
Android N and later uses Java 8 syntax because Google is moving from the proprietary JDK framework to the OpenJDK framework
You should already have a basic knowledge of programming constructs from any programming language. They include, variables and arrays, conditional statements, functions and loops.
In this tutorial we will mainly highlight concepts that are UNIQUE to Java and OOP. If you need knowledge of basic programming constructs, see my app “The ABCs of Programming Constructs” shown on the left.
Object-Oriented Programming (OOP) is NOT a programming language but a methodology (a particular set of methods or rules for programming). While OOP programming concepts and constructs are similar to all languages, where they differ is in their syntax (the way they are written).
There are three key terms that you need to know when dealing with Object Oriented Programming.
Hence, properties and methods allow an object to be CUSTOMIZED. Properties give unique attributes to an object instead of having each instance of an object having the same attributes. Attributes (e.g., color, height, width) describes the CURRENT STATE of an object. Objects obtain interaction with the outside objects through their methods that they expose.
Below is a summary of some of the most common languages and what they support:
Language Type | Create Inheritance | Use Typing |
Call to super | Use Private Methods | Use Abstract Classes | Implement Interface |
---|---|---|---|---|---|---|
Java | Single | static | super | Yes | Yes | Yes |
C# | Single | static | base | Yes | Yes | Yes |
VB.NET | Single | static | MyBase | Yes | Yes | Yes |
Objective-C | Single | static/dynamic | super | No | No | Protocols |
C++ | Multiple | static | class name | Yes | Yes | Abstract Class |
Ruby | Mix-ins | dynamic | super | Yes | N/A | N/A |
JavaScript | Prototype | dynamic | N/A | Yes | N/A | N/A |
Class—is a SELF-CONTAINED set of instructions (properties and methods) on how to CREATE an object. Hence, a class is a BLUEPRINT on how to build an object. It is not the object but a set of instructions on HOW TO build an object. If you are building a house from a blueprint, you would not set the blueprint on the carpet in your office and stand on it and say this is where my bed room is.
Object—once an object is created, it contains ALL of the properties and methods of its class and it works independent of other objects create by the same class.
Abstraction— to “abstract” (express a quality or characteristic apart from any specific object or instance) the ESSENTIAL ELEMENTS of an object. For example, we do not want to create a separate class for a small house or a separate class for a large house but simple ONE class that has characteristics COMMON to ANY house.
Encapsulation—to “encapsulate” (to become enclosed in as if in a capsule) an object properties and methods INSIDE of a class definition to protect it from the outside. This is akin to an encapsulated pill that protects the content of the medicine on the inside of the capsule. Encapsulation also “hides” the internal working on an object. For example, you don’t know or you don’t care how the internal mechanics of a car engine works—all you care is that the engine works when you get into it to drive it. Encapsulation encloses the internal workings of an object in a “black box” of sort. The object should not reveal anything about itself other than what is actually needed by other objects. For example, the engine reveals its gears to the transmission which reveals itself to the axles that in turn turns the wheels.
Inheritance—instead of creating a similar class from start-to-finish, inheritance allows you to create another class that inherits all of the properties and methods of its parents. However, the child class can have its own set of properties and methods independent of its parent class. Also, that object can override properties and methods of its parent class. In real life, children inherit properties (e.g., skin, hair and eye color) of their parents unless they define their own properties (e.g., change their hair color). The relationship between the parent and child class is called the Superclass (parent) and the Subclass (child), respectively. In it important to note that when changes are made to the parent class, those changes may cascade to any children classes because they are based on the parent class. Inheritance allows you to reuse existing code.
Polymorphisim—“poly” means “many” and “morph” means “form” so polymorphism is creating “many forms” of the same class by OVERRIDING (modifying existing) properties and methods of its parent class OR by ADDING NEW properties and methods that does not exist in its parent class to create a custom class from it in another form. For example, a House class that was based on its parent class can override a property called elevation to change the look of a custom house.
Knowing all of these concepts will help you to make your code more efficient. For example, creating multiple small objects instead of one large object has the following advantages:
There are several steps that you can used to determine how to build an app:
Determine the functional (e.g., features) and non-functional (e.g., help) requirements that the app MUST have not like to have. A common practice is to use the acronym FURPS+ when referring to these requirements:
Once you have established a list of "must-have" requirements, you can now set your attention on how a user will use the app by writing a use case diagram that has a:
User Story is simpler than a use case and it has the following syntax that are typically written on index cards:
EXAMPLE:
Now that you have determine the requirements for an app and the users of the app, it is time to identify classes that we can create and show how they relate to one another.
REPLACE LATER WITH OWN SCREENSHOT:
NOTE: This is similar to database development where you:
Like nouns that were used to identify the potential classes needed, now we will look for verbs or verb phrases in our scenario to ASSIGN potential responsibilities to methods for that class.
NOTE: It is sometimes difficult to determine where to assign a responsibility (method) to what class. For example, if you have a responsibility called "Check Order", do you place that responsibility on the Order or Customer class? It is important to remember that an object is responsible for itself. So even though the customer may want to know the status of an order, it is the responsibility of the Order class to do that function (method)—getOrder().
REPLACE LATER WITH OWN SCREENSHOT:
NOTE: It is important to note that the screenshot is not showing who initiate these functions (e.g., customer) but where the responsibilities lies for performing them (e.g., order). While the customer may be initiating actions like get order or confirm order, these actions are ACTUALLY performed in the Order class.
As an alternative to identifying classes, you could use a simpler approach called CRC (Class, Responsibility and Collaboration) Cards that are written on index cards with the Class name on top and then the Responsibilities taking about 2/3 of the card and collaborators taking 1/3 of the card:
REPLACE LATER WITH OWN SCREENSHOT
EXAMPLES:
Now, you are ready to create classes using a class diagram which includes:
Steps in writing a class diagrams:
TIP: Many IDEs will automatically generates getters and setters. Many IDEs will automatically generates getters and setters.Many IDEs will automatically generates getters and setters.Many IDEs will automatically generates getters and setters.
REPLACE LATER WITH OWN SCREENSHOT
EXAMPLE OF A CLASS DIAGRAM:
Once you have a class diagram, you can use it to create Java code from it.
REPLACE LATER WITH OWN SCREENSHOT
EXAMPLE:
From this:
To this:
USE MY CODE:
class MyBox {
int height=100;
int width=100;
string color = "blue";
void setHeight (int newHeight) {
height = newHeight;
}
void setWidth (int newWidth) {
height = newWidth;
}
void setColor (int newColor) {
color = newColor;
}
void printProperties () {
System.out.println("Box height: " + height + "Box width: " + width + "Box color: " + color);
}
}
NOTES:
class MyBoxDemo {
public static void main(String[] args) {
// Create two boxes
MyBox box1 = new MyBox();
MyBox box2 = new MyBox();
// Invoke box's methods:
box1.newHeight(50);
box1.newWidth(50);
box1.newColor("red");
box1.printProperties();
box2.newHeight(50);
box2.newWidth(50);
box2.newColor("green");
box2.printProperties();
}
}
The output should read:
Box height: 50 Box width: 50 Box color: red
Box height: 25 Box width: 25 Box color: green
NOTES:
QUESTION: What would happen if you don't pass arguments to the methods when the object is instantiated (e.g., box1.newHeight())?
While the above code works, it will generate an object AND then you have to assign STATES to it. We will see later how to use a constructor method to define the STATES when the object is created.
NOTES:
Designed Patterns are well-tested best-practices solutions to common software development. For example, there is:
Design Patterns are best known from the book Design Patterns (often referred to as the Gang of Four) that details 23 Design Patterns split in three categories:
Creational Patterns—deals with the creation of objects
Structural Patterns—deals with how classes are designed
Behavioral Patterns—deals with communication between objects
RETYPE IN TEXT
Below is a list of general principles
Unnecessary and duplicated code is referred to a Code Smell.
Code smell, also known as bad smell, in computer programming code, refers to any symptom in the source code of a program that possibly indicates a deeper problem.[1] According to Martin Fowler,[2] "a code smell is a surface indication that usually corresponds to a deeper problem in the system". Another way to look at smells is with respect to principles and quality:[3] "smells are certain structures in the code that indicate violation of fundamental design principles and negatively impact design quality". Code smells are usually not bugs—they are not technically incorrect and do not currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.
Determining what is and is not a code smell is subjective, and varies by language, developer and development methodology. There are tools, such as Checkstyle, PMD and FindBugs for Java, to automatically check for certain kinds of code smells.
Application-level smells:
Class-level smells:
Method-level smells:
The SOLID Principle by Robert Martin is not a set of rules but guidelines and should be treated as a checklist to create a class design.
Single Responsibility Principle
Open/Close Principle
Liskov Substitution Principle
Integration Segregation Principle
Dependency Inversion Principle
GRASP (General Responsibility Assignment Software Pattern) is a set of OO design principles that focus on RESPONSIBILITIES. Like the SOLID principle it is a NOT a set of rules but GUIDELINES when creating UML diagrams
Who created this object?
Who is responsible for how these objects talk to each other?
Who will take care of passing messages received from the UI?
There are nine patterns on GRASP:
Below is a list of resource books that you can use: