Hello! In this course, we will take a detailed look at one of the important concepts in Android app development using Java: ‘Inheritance’. Inheritance is a core element of object-oriented programming, which greatly helps in increasing code reusability and expressing hierarchical relationships. This course will comprehensively cover the concept of inheritance, how to use it, and practical examples in Android.
1. What is Inheritance?
Inheritance means that one class inherits the properties and functionalities of another class in object-oriented programming. This allows us to reuse the code and functionality of existing classes when defining new classes, reducing code duplication and making maintenance easier.
2. Basic Structure of Inheritance
To inherit a class in Java, the ‘extends’ keyword is used. A child class (subclass) that inherits properties from the parent class (superclass) can have its own additional fields and methods. The basic structure of inheritance is as follows:
class ParentClass {
// Fields and methods of the parent class
}
class ChildClass extends ParentClass {
// Additional fields and methods of the child class
}
3. Advantages of Inheritance
- Code Reusability: You can easily create new classes by reusing existing ones.
- Ease of Maintenance: By placing common functionalities in the parent class and modifying them, you can change the behavior of child classes.
- Readability: It improves the readability of the code by clearly expressing the relationships between classes.
4. Example of Inheritance
Now, we will demonstrate the structure of inheritance by creating a simple Android app. In this hypothetical app, we will create a basic ‘User’ class and subclasses ‘Admin’ and ‘RegularUser’ that inherit from it.
4.1 Creating the User Class
public class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Email: " + email);
}
}
4.2 Creating the Admin Class
public class Admin extends User {
private String department;
public Admin(String name, String email, String department) {
super(name, email); // Calling the parent class constructor
this.department = department;
}
public String getDepartment() {
return department;
}
@Override
public void displayInfo() {
super.displayInfo(); // Calling the parent's method
System.out.println("Department: " + department);
}
}
4.3 Creating the Regular User Class
public class RegularUser extends User {
private String username;
public RegularUser(String name, String email, String username) {
super(name, email);
this.username = username;
}
public String getUsername() {
return username;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Username: " + username);
}
}
5. Using Inheritance in Android Apps
Android applications often abstract common behaviors that occur among multiple classes. For example, if multiple activities need to perform common actions, you can create a base activity class and inherit from it.
5.1 Creating a Base Activity Class
public class BaseActivity extends AppCompatActivity {
protected void setContentViewWithToolbar(int layoutId) {
setContentView(layoutId);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
5.2 Creating a User Management Activity
public class UserManagementActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentViewWithToolbar(R.layout.activity_user_management);
// Initialization related to user management
}
}
5.3 Creating a Settings Activity
public class SettingsActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentViewWithToolbar(R.layout.activity_settings);
// Initialization related to settings
}
}
6. Inheritance and Polymorphism
Inheritance supports polymorphism. In Java, you can use a variable of the parent class type to reference an object of a child class. This allows you to use subclass objects as if they were superclass objects.
public void displayUserInfo(User user) {
user.displayInfo(); // Example of polymorphism
}
7. Conclusion
Today, we learned about inheritance in Android app development using Java. I hope you understood how to enhance code reusability and enable structural design through inheritance. In software development, inheritance is a crucial aspect, and utilizing it effectively can lead to more efficient code writing. In the next session, we will cover abstract classes and interfaces. Thank you!