English subtitles

← Inheriting Behavior

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Let's go back to the JustJava app for
    just a moment.
  2. Let's look at the MainActivity
    class displayMessage method.
  3. Things are starting to make
    a little more sense in here.
  4. We already recognize that we're creating
    a variable called orderSummaryTextView.
  5. The data type of this
    variable was TextView.
  6. We didn't understand this part yet
  7. On the next line, we call a method
    on this text view object.
  8. We use the same variable
    name as declared up here.
  9. So we call orderSummaryTextView.setText.
  10. Then we pass a string as
    an input to the method.
  11. This string message originally
    came as an input parameter
  12. to the display message method.
  13. Now let's dig in to this part of
    the code, which we don't understand yet.
  14. It looks like findViewById
    is a method call, though.
  15. Because its preceding these parenthesis
    with what looks like an input argument.
  16. But, what's weird is that this method
    isn't defined anywhere in this class.
  17. If I double-click on this name and
    then hit command F, or
  18. control-F if you're on Windows, then
    we can try to do a search in this file.
  19. I only see two occurrences
    of findViewById, and
  20. they're used in similar ways.
  21. But I don't see any method
    that's called findViewById.
  22. Speaking of which,
    earlier in the main activity class,
  23. we see a call to setContentView,
    but if you search for
  24. this in the file, it's also not
    defined in the main activity.
  25. So, where are these methods defined?
  26. Well, MainActivity is
    actually a pretty short file.
  27. It's only about 90 lines long,
  28. but there's actually more to
    this class than meets the eye.
  29. The code says,
    extends AppCompatActivity.
  30. This means that the main
    activity is an extension
  31. of the functionality in
    the AppCompatActivity class.
  32. On your computer if you see action
    bar activity here, that's okay,
  33. that's just an older version of this.
  34. The latest version that you should
    be using is AppCompatActivity.
  35. And AppCompatActivity gives us
    backward compatibility support
  36. on older Android devices.
  37. The AppCompatActivity is part
    of the Android support library.
  38. It allows us to use the latest
    UI features on Android,
  39. while still working on
    older Android devices.
  40. By extending
    the AppCompatActivity class,
  41. we're getting all the functionality,
    all the state and
  42. the methods from here within
    the MainActivity for free.
  43. We don't have to copy and
    paste any code from here.
  44. We can just simply extend that class.
  45. If you're interested,
  46. you can find out more information on
    AppCompatActivity by googling for it.
  47. Here's the reference doc for
    the AppCompatActivity class.
  48. Since I have the Chrome
    extension installed,
  49. I can view the source as well.
  50. And this is the code for where
    the AppCompatActivity class is defined.
  51. You can see that there's a lot
    of functionality here, but
  52. the key message is that you don't have
    to understand how it's implemented.
  53. All you need to know is that when
    you extend AppCompatActivity,
  54. you get all of this functionality for
  55. At a high level you can
    visualize it like this.
  56. This is a class definition for
  57. And say you have some
    methods defined here.
  58. When you extend AppCompatActivity
    then we can access the state and
  59. then the methods from AppCompatActivity.
  60. Now, they're not physically added
    to the main activity class, but
  61. you can imagine that they're there,
  62. because you can still reference
    the state and the methods as well.
  63. And that's how our code can refer
    to things like setContentView or
  64. findViewById.
  65. Because we inherited those methods, we
    didn't define them in the MainActivity.
  66. The MainActivity is shown as
    a screen on the device, but
  67. we didn't write the code for that in the
    MainActivity, we inherited that as well.
  68. We don't have the state and the methods
    from AppCompatActivity directly
  69. in the class, but now we know that
    they're there and we can refer to them.
  70. Speaking of inheriting things,
    there's good and bad parts about that.
  71. Sometimes we'll want to
    inherit behavior but
  72. other times we might want
    to modify it slightly.
  73. If you want to change some of
    the behavior from this inherited class,
  74. you can override certain methods.
  75. You can add this @override text on top
    of a method so that the computer knows
  76. that you don't want the version of
    the method from the AppCompatActivity.
  77. But instead, you want this
    version that you've defined here.
  78. This is a tricky topic, and
    it requires a lot of practice.
  79. But we have seen one example
    of overriding a method
  80. from the AppCompatActivity class.
  81. And that method is the on create method.
  82. By overriding the on create method in
    the MainActivity, we're specifying our
  83. own implementation for what should
    happen when that method is called.
  84. Don't worry if you don't
    understand this right away.
  85. I'm just giving you a brief overview
    of a bunch of object-oriented
  86. programming concepts.
  87. It's absolutely expected that you
    would still have lingering questions.
  88. And still need to read up more
    these topics in order to fully
  89. understands them.
  90. Another way to visualize the
    relationship between the MainActivity
  91. and the AppCompatActivity classes is
    to draw a Class Hierarchy Diagram.
  92. This is different from
    a View Hierarchy Diagram, because
  93. the View Hierarchy Diagram shows a tree
    of views that are shown on a screen.
  94. The Class Hierarchy Diagram
    shows the relationship
  95. between different Java classes.
  96. We have AppCompatActivity as superclass.
  97. Since MainActivity extends
    from AppCompatActivity,
  98. then this is known as the subclass.
  99. So whenever you see this class
    definition whatever you extend is known
  100. as the superclass.
  101. This would be the subclass.
  102. So if you created another activity
    called detail activity and
  103. you extended AppCompatActivity,
    this would be the subclass, and
  104. this would be the superclass.
  105. Here's another example of
    Java class inheritance.
  106. If you think about it,
    the text view, image view, and
  107. button views all have some
    common aspects to them.
  108. All of these views have a width and
    a height on the screen.
  109. We haven't learned this yet, but
    they also have a visibility state.
  110. So they can all be made invisible or
  111. There are a couple of other properties
    as well, that are common among them.
  112. Instead of writing out code for these
    properties within the text view class,
  113. and then copying it over to image view,
    and then button class.
  114. We can do something a little smarter.
  115. We can create a view class.
  116. We can extract the common properties
    among all of these views and then put
  117. them inside this view class that way
    the code only has to be written once.
  118. But then how do we make the connection
    between the text view class and
  119. the view class?
  120. If you're thinking inheritance,
    you're correct.
  121. In the TextView java file we're
    going to define the text view class.
  122. That way we'll inherit all this data and
    the methods from the view class.
  123. Then we won't have to have as
    much code in the TextView class
  124. because part of the functionality is
    already written in the view class.
  125. The same would apply for
    the ImageView and
  126. Button classes,
    we don't have to write as much code.
  127. In these files, we only have to
    write about what's different
  128. about the ImageView
    compared to the View class.
  129. I think of this as specifying a delta,
    so the ImageView class only has to
  130. specify what's different about the
    ImageView compared to the View class.
  131. If we don't want to inherit certain
    behavior from the View class,
  132. we can just specify the updated
    behavior within the ImageView class, or
  133. any of these classes.
  134. To remind you of the terminology,
    in this case,
  135. TextView would be the subclass,
    and View would be the superclass.
  136. If we go back to the documentation for
  137. the TextView class,
    we actually understand this part now.
  138. It says TextView extends view.
  139. Now we know that it means that
    we're inheriting the state and
  140. the methods from the ViewClass.
  141. And down here is a class
    hierarchy diagram.
  142. It shows that the TextView class
    is inheriting from the ViewClass.
  143. And in turn, the View class
    inherits from the Object class.
  144. If you explore around,
    you can find other classes,
  145. like the EditText class, which actually
    extends from the TextView class.
  146. So it takes the behavior
    form the TextView class and
  147. then adds a little more
    specific functionality to it.
  148. So EditText extends from TextView.
  149. And TextView extends from View,
    and View extends from Object.
  150. And this is the class hierarchy diagram.
  151. There's a lot going on, and
    you can look at it for hours.
  152. But let me just show you one thing
    on the TextView documentation page.
  153. I want to show you the method for
  154. Here's the Java method for
    SetVisibility within the TextView class.
  155. This changes whether
    the view is visible or not.
  156. If I click on this method,
    it actually brings me to the View class.
  157. And then it describes
    what this method does.
  158. So you can see that a TextView
    object has a set visibility method.
  159. But it was actually inherited
    from the View class.
  160. So we got this functionality for
    free, from the View class.
  161. Okay, we just covered
    a ton of new concepts.
  162. Normally this would be spread out over
    a semester of a computer science course.
  163. We're going to do a little
    bit of practice now and
  164. hopefully it will start to
    connect the dots for you.
  165. But know that it will
    take a lot more time and
  166. practice until you fully
    grasp these concepts.
  167. Okay, first exercise, I want you
    to create another new Android app,
  168. because we don't want to
    mess up the just Java app.
  169. Once you create the project,
    then run the app.
  170. Once you create your app,
    you're going to see a MainActivity file.
  171. That MainActivity file will
    extend from AppCompatActivity.
  172. In your version of Android Studio,
    if the MainActivity extends from
  173. ActionBarActivity instead,
    that's okay too.
  174. That's just an older version
    of AppCompatActivity.
  175. By removing this method override,
    we're going to fall back to
  176. the way that onCreate method appeared
    in the AppCompatActivity class.
  177. The presence of this method says we want
    different behavior in the MainActivity
  178. for the onCreate.
  179. So removing it brings us back
    to the original behavior in
  180. the AppCompatActivity class.
  181. When you run the app,
  182. what do you notice is different
    compared to when you first ran the app?
  183. Write your answer in this text box.