Hello! In this tutorial, we will take an in-depth look at two key concepts in Android app development using the Java language: ‘Class’ and ‘Constructor’. Java is an object-oriented programming language, and classes and objects play a very important role in Java. This allows us to improve code reusability and maintainability.
1. What is a Class?
A class is the basic unit of object-oriented programming and serves as a blueprint for creating objects. A class defines an object by bundling data (attributes) and methods (functions) together. For example, let’s assume we create a class called ‘Car’. This class can include the car’s attributes (e.g., brand, color, model) and functionalities (e.g., acceleration, deceleration, parking).
1.1. Basic Structure of a Class
A class in Java has the following structure:
public class Car {
// Attributes
String brand;
String color;
int model;
// Constructor
public Car(String brand, String color, int model) {
this.brand = brand;
this.color = color;
this.model = model;
}
// Method
public void accelerate() {
System.out.println("The car is accelerating.");
}
}
2. What is a Constructor?
A constructor is a special method that is called when an object is created. The main role of a constructor is to handle the initialization of the object. The constructor has the same name as the class and does not have a return type.
2.1. Types of Constructors
In Java, constructors are broadly classified into two types:
- Default Constructor: A constructor with no arguments. It initializes the attributes of the class to their default values.
- Parameterized Constructor: A constructor that takes parameters to initialize the attributes of the object.
2.2. Example of Constructors
Let’s look at examples of the default constructor and the parameterized constructor:
public class Car {
String brand;
String color;
int model;
// Default Constructor
public Car() {
this.brand = "Undefined";
this.color = "Undefined";
this.model = 0;
}
// Parameterized Constructor
public Car(String brand, String color, int model) {
this.brand = brand;
this.color = color;
this.model = model;
}
}
3. Developing an Android App Using Classes and Constructors
Now, let’s create a simple Android application based on the concepts of classes and constructors. This application will have the functionality to input and output car information.
3.1. Creating an Android Studio Project
Launch Android Studio and create a new project. Select ‘Empty Activity’ and name it ‘MainActivity’.
3.2. MainActivity.java Code Example
In the MainActivity.java file, add the following code to create an app that receives and outputs car information:
package com.example.carapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText inputBrand, inputColor, inputModel;
private TextView outputText;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputBrand = findViewById(R.id.inputBrand);
inputColor = findViewById(R.id.inputColor);
inputModel = findViewById(R.id.inputModel);
outputText = findViewById(R.id.outputText);
submitButton = findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String brand = inputBrand.getText().toString();
String color = inputColor.getText().toString();
int model = Integer.parseInt(inputModel.getText().toString());
Car car = new Car(brand, color, model);
outputText.setText("Car Information:\nBrand: " + car.brand + "\nColor: " + car.color + "\nModel: " + car.model);
}
});
}
}
3.3. Setting Up activity_main.xml Layout
Add the following UI elements to the activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/inputBrand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Brand"/>
<EditText
android:id="@+id/inputColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Color"/>
<EditText
android:id="@+id/inputModel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Model"/>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
<TextView
android:id="@+id/outputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"/>
</LinearLayout>
4. Utilizing Classes and Constructors
Through the example above, we can understand how classes and parameterized constructors work. When the user inputs the car’s brand, color, and model, a new Car object is created, and that information is displayed in the text view. The advantage of object-oriented programming is that it allows for code reusability and efficient management of data and functionality through objects.
5. Conclusion
In this tutorial, we have taken an in-depth look at the concepts of classes and constructors in Java. Classes and constructors play a central role in object-oriented programming and are essential elements in Android app development. In future tutorials, we will cover a variety of features and examples, so please look forward to it. Happy Coding!