Object-Oriented Programming for Android Apps - dummies

Object-Oriented Programming for Android Apps

By Barry Burd

Object-oriented programming has brought about a big change in application development and programming. An object-oriented program is, at its heart, designed to be modified. Using correctly written software, you can take advantage of features that are already built in, add new features of your own, and override features that don’t suit your needs.

The best aspect of this situation is that the changes you make are clean — no clawing and digging into other people’s brittle program code. Instead, you make nice, orderly additions and modifications without touching the existing code’s internal logic. It’s the ideal solution.

When you write an object-oriented program, you start by considering the data. Say you’re writing about accounts. So what’s an account? You’re writing code to handle button clicks. So what’s a button? You’re writing a program to send payroll checks to employees. What’s an employee?

For example, say an employee is someone with a name and a job title — sure, employees have other characteristics, but for now stick to the basics:

class Employee {
  String name;
  String jobTitle;

Of course, any company has different kinds of employees. For example, your company may have full-time and part-time employees. Each full-time employee has a yearly salary:

class FullTimeEmployee extends Employee {
  double salary;

In this example, the words extends Employee tell Java that the new class (the FullTimeEmployee class) has all the properties that any Employee has and, possibly, more. In other words, every FullTimeEmployee object is an Employee object (an employee of a certain kind, perhaps).

Like any Employee, a FullTimeEmployee has a name and a jobTitle. But a FullTimeEmployee also has a salary. That’s what the words extends Employee do for you.

A part-time employee has no fixed yearly salary. Instead, every part-time employee has an hourly pay rate and a certain number of hours worked in a week:

class PartTimeEmployee extends Employee {
  double hourlyPay;
  int hoursWorked;

So far, a PartTimeEmployee has four characteristics: name, jobTitle, hourlyPay, and number of hoursWorked.

Then you have to consider the big shots — the executives. Every executive is a full-time employee. But in addition to earning a salary, every executive receives a bonus (even if the company goes belly-up and needs to be bailed out):

class Executive extends FullTimeEmployee {
  double bonus;

Java’s extends keyword is cool because, by extending a class, you inherit all the complicated code that’s already in the other class. The class you extend can be a class that you have (or another developer has) already written. One way or another, you’re able to reuse existing code and to add ingredients to the existing code.

Here’s another example: The creators of Android wrote the Activity class, with its 5,000 lines of code. You get to use all those lines of code for free by simply typing extends Activity:

public class MainActivity extends Activity {

With the two words extends Activity, your new MainActivity class can do all the things that a typical Android activity can do — start running, find items in the app’s res directory, show a dialog box, respond to a low-memory condition, start another activity, return an answer to an activity, finish running, and much more.