Inheritance

Most of the classes you will write while creating an Android application will be subclasses of the classes created by Google and provided in the Android SDK (such as Activity and View). We’re going to discuss some of the basic principles of single inheritance, interfaces, and polymorphism, and how they are implemented in Java.

Inheritance

Wikipedia defines Inheritance as such:

“In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both… In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes…. The resulting classes are known as… subclasses… The relationships of classes through inheritance gives rise to a hierarchy.”

So what does this mean for us? A class can have exactly 1 direct ancestor, and can implement as many interfaces as needed.

extends vs implements

In Java to create a subclass of an abstract or concrete class, you would declare your class like this:

public class MyActivity extends Activity {
}

We now have a class called MyActivity which is an Activity, and has new exciting behavior in addition to what is defined in Activity.

Want your class to implement an interface? Do this:

public class MyActivity extends Activity implements Runnable {
}

Now we have a Runnable Activity.

This is all pretty basic. Nothing to write home about. Basic inheritance. If you want to know more, Google it.

Often times we need an implementation of some interface for a single use. Instead of defining a new file and a new class just for an object we will only ever use once, we can create an on-the-fly implementation of that interface. (If you’ve ever written javascript this will be familiar)

Button b = new Button(...);
b.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
      // do stuff
   }
});

That’s convenient isn’t it? Not quite as elegant as lambda expressions or delegates in C#, but its pretty good. One of the best parts about it is that classes defined in this way have access to the variables in its enclosing class

public class TestClass {
   private String mString;
   public void SomeMethod(Button b)
   {
      b.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            Log.w(mString, "WORD UP!");
         }
      });
   }
}

As you can see in the above example, our on-the-fly implementation of OnClickListener has access to mString.

This is also true of private internal classes. In Java you can define a class within a class that can only be used by the enclosing class. This internal class does not have to inherit from its enclosing class, it merely provides a scope for the class. One practical use for this is implementing a linked list.

public class TestClass {

   private class Link
   {
      public Link link;
      public Object value;
   }

   private Link head;
   
   public TestClass()
   {
      head = new Link();
   }
}

In the example above the class Link is only available to TestClass. Other people using TestClass cannot see or use the Link class. Internal classes can also be public if there is reason for the consumer to have access. One such example of this is the LayoutParams class. All of the Layout classes (such as LinearLayout and RelativeLayout) have internal classes called LayoutParams, which can be instatiated as:

new LinearLayout.LayoutParams()

or

new RelativeLayout.LayoutParams()

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: