-
Title:
Inheriting Behavior
-
Description:
-
Let's go back to the JustJava app for
just a moment.
-
Let's look at the MainActivity
class displayMessage method.
-
Things are starting to make
a little more sense in here.
-
We already recognize that we're creating
a variable called orderSummaryTextView.
-
The data type of this
variable was TextView.
-
We didn't understand this part yet
though.
-
On the next line, we call a method
on this text view object.
-
We use the same variable
name as declared up here.
-
So we call orderSummaryTextView.setText.
-
Then we pass a string as
an input to the method.
-
This string message originally
came as an input parameter
-
to the display message method.
-
Now let's dig in to this part of
the code, which we don't understand yet.
-
It looks like findViewById
is a method call, though.
-
Because its preceding these parenthesis
with what looks like an input argument.
-
But, what's weird is that this method
isn't defined anywhere in this class.
-
If I double-click on this name and
then hit command F, or
-
control-F if you're on Windows, then
we can try to do a search in this file.
-
I only see two occurrences
of findViewById, and
-
they're used in similar ways.
-
But I don't see any method
that's called findViewById.
-
Speaking of which,
earlier in the main activity class,
-
we see a call to setContentView,
but if you search for
-
this in the file, it's also not
defined in the main activity.
-
So, where are these methods defined?
-
Well, MainActivity is
actually a pretty short file.
-
It's only about 90 lines long,
-
but there's actually more to
this class than meets the eye.
-
The code says,
extends AppCompatActivity.
-
This means that the main
activity is an extension
-
of the functionality in
the AppCompatActivity class.
-
On your computer if you see action
bar activity here, that's okay,
-
that's just an older version of this.
-
The latest version that you should
be using is AppCompatActivity.
-
And AppCompatActivity gives us
backward compatibility support
-
on older Android devices.
-
The AppCompatActivity is part
of the Android support library.
-
It allows us to use the latest
UI features on Android,
-
while still working on
older Android devices.
-
By extending
the AppCompatActivity class,
-
we're getting all the functionality,
all the state and
-
the methods from here within
the MainActivity for free.
-
We don't have to copy and
paste any code from here.
-
We can just simply extend that class.
-
If you're interested,
-
you can find out more information on
AppCompatActivity by googling for it.
-
Here's the reference doc for
the AppCompatActivity class.
-
Since I have the Chrome
extension installed,
-
I can view the source as well.
-
And this is the code for where
the AppCompatActivity class is defined.
-
You can see that there's a lot
of functionality here, but
-
the key message is that you don't have
to understand how it's implemented.
-
All you need to know is that when
you extend AppCompatActivity,
-
you get all of this functionality for
free.
-
At a high level you can
visualize it like this.
-
This is a class definition for
MainActivity.
-
And say you have some
methods defined here.
-
When you extend AppCompatActivity
then we can access the state and
-
then the methods from AppCompatActivity.
-
Now, they're not physically added
to the main activity class, but
-
you can imagine that they're there,
-
because you can still reference
the state and the methods as well.
-
And that's how our code can refer
to things like setContentView or
-
findViewById.
-
Because we inherited those methods, we
didn't define them in the MainActivity.
-
The MainActivity is shown as
a screen on the device, but
-
we didn't write the code for that in the
MainActivity, we inherited that as well.
-
We don't have the state and the methods
from AppCompatActivity directly
-
in the class, but now we know that
they're there and we can refer to them.
-
Speaking of inheriting things,
there's good and bad parts about that.
-
Sometimes we'll want to
inherit behavior but
-
other times we might want
to modify it slightly.
-
If you want to change some of
the behavior from this inherited class,
-
you can override certain methods.
-
You can add this @override text on top
of a method so that the computer knows
-
that you don't want the version of
the method from the AppCompatActivity.
-
But instead, you want this
version that you've defined here.
-
This is a tricky topic, and
it requires a lot of practice.
-
But we have seen one example
of overriding a method
-
from the AppCompatActivity class.
-
And that method is the on create method.
-
By overriding the on create method in
the MainActivity, we're specifying our
-
own implementation for what should
happen when that method is called.
-
Don't worry if you don't
understand this right away.
-
I'm just giving you a brief overview
of a bunch of object-oriented
-
programming concepts.
-
It's absolutely expected that you
would still have lingering questions.
-
And still need to read up more
these topics in order to fully
-
understands them.
-
Another way to visualize the
relationship between the MainActivity
-
and the AppCompatActivity classes is
to draw a Class Hierarchy Diagram.
-
This is different from
a View Hierarchy Diagram, because
-
the View Hierarchy Diagram shows a tree
of views that are shown on a screen.
-
The Class Hierarchy Diagram
shows the relationship
-
between different Java classes.
-
We have AppCompatActivity as superclass.
-
Since MainActivity extends
from AppCompatActivity,
-
then this is known as the subclass.
-
So whenever you see this class
definition whatever you extend is known
-
as the superclass.
-
This would be the subclass.
-
So if you created another activity
called detail activity and
-
you extended AppCompatActivity,
this would be the subclass, and
-
this would be the superclass.
-
Here's another example of
Java class inheritance.
-
If you think about it,
the text view, image view, and
-
button views all have some
common aspects to them.
-
All of these views have a width and
a height on the screen.
-
We haven't learned this yet, but
they also have a visibility state.
-
So they can all be made invisible or
visible.
-
There are a couple of other properties
as well, that are common among them.
-
Instead of writing out code for these
properties within the text view class,
-
and then copying it over to image view,
and then button class.
-
We can do something a little smarter.
-
We can create a view class.
-
We can extract the common properties
among all of these views and then put
-
them inside this view class that way
the code only has to be written once.
-
But then how do we make the connection
between the text view class and
-
the view class?
-
If you're thinking inheritance,
you're correct.
-
In the TextView java file we're
going to define the text view class.
-
That way we'll inherit all this data and
the methods from the view class.
-
Then we won't have to have as
much code in the TextView class
-
because part of the functionality is
already written in the view class.
-
The same would apply for
the ImageView and
-
Button classes,
we don't have to write as much code.
-
In these files, we only have to
write about what's different
-
about the ImageView
compared to the View class.
-
I think of this as specifying a delta,
so the ImageView class only has to
-
specify what's different about the
ImageView compared to the View class.
-
If we don't want to inherit certain
behavior from the View class,
-
we can just specify the updated
behavior within the ImageView class, or
-
any of these classes.
-
To remind you of the terminology,
in this case,
-
TextView would be the subclass,
and View would be the superclass.
-
If we go back to the documentation for
-
the TextView class,
we actually understand this part now.
-
It says TextView extends view.
-
Now we know that it means that
we're inheriting the state and
-
the methods from the ViewClass.
-
And down here is a class
hierarchy diagram.
-
It shows that the TextView class
is inheriting from the ViewClass.
-
And in turn, the View class
inherits from the Object class.
-
If you explore around,
you can find other classes,
-
like the EditText class, which actually
extends from the TextView class.
-
So it takes the behavior
form the TextView class and
-
then adds a little more
specific functionality to it.
-
So EditText extends from TextView.
-
And TextView extends from View,
and View extends from Object.
-
And this is the class hierarchy diagram.
-
There's a lot going on, and
you can look at it for hours.
-
But let me just show you one thing
on the TextView documentation page.
-
I want to show you the method for
SetVisibility.
-
Here's the Java method for
SetVisibility within the TextView class.
-
This changes whether
the view is visible or not.
-
If I click on this method,
it actually brings me to the View class.
-
And then it describes
what this method does.
-
So you can see that a TextView
object has a set visibility method.
-
But it was actually inherited
from the View class.
-
So we got this functionality for
free, from the View class.
-
Okay, we just covered
a ton of new concepts.
-
Normally this would be spread out over
a semester of a computer science course.
-
We're going to do a little
bit of practice now and
-
hopefully it will start to
connect the dots for you.
-
But know that it will
take a lot more time and
-
practice until you fully
grasp these concepts.
-
Okay, first exercise, I want you
to create another new Android app,
-
because we don't want to
mess up the just Java app.
-
Once you create the project,
then run the app.
-
Once you create your app,
you're going to see a MainActivity file.
-
That MainActivity file will
extend from AppCompatActivity.
-
In your version of Android Studio,
if the MainActivity extends from
-
ActionBarActivity instead,
that's okay too.
-
That's just an older version
of AppCompatActivity.
-
By removing this method override,
we're going to fall back to
-
the way that onCreate method appeared
in the AppCompatActivity class.
-
The presence of this method says we want
different behavior in the MainActivity
-
for the onCreate.
-
So removing it brings us back
to the original behavior in
-
the AppCompatActivity class.
-
When you run the app,
-
what do you notice is different
compared to when you first ran the app?
-
Write your answer in this text box.