There are three levels of access: private, protected, and
public
Private methods may only be called by another instance of the
class.
A class's protected methods may only be called objects which
subclass the
class, or objects whose class are in the same
package.
Public methods may be called by any other
object.
Private, protected, and public have corresponding, though not
exactly the
same, meanings when applied to Classes and variables.
Final
means cannot be changed, generally. For instance a method marked
"public final"
means it can be accessed by any object, but objects of a subclass
cannot
override it. So what's overriding? Let's say we are writing a
library for
simulating animal behavior. One thing we could do is create an
Animal class and
put in an abstract method "public abstract void makeNoise()".
It's abstract
because we don't write any code here. We then create our Cat class
which
subclasses Animal. Because Cat is a concrete class, we have to write the
code
to implement public void makeNoise() [the abstract disappears because
there is
implementing code.] We'll do the obvious { System.out.printLn("Meow");
}. Now
we make a concrete Dog class, again subclassing Animal, and again
implementing
makeNoise, as follows: public void makeNoise() {
System.out.println("Woof.");
} One might ask, so? This is called
polymorphism, by the way, and it
allows us to abstract the specific animal type,
because whether the Animal is
Dog, Cat, Duck, or CartoonPlatypus, we know we
can call makeNoise() because
that comes being a subclass of
the Animal class. Now we could have made
the makeNoise method in
Animal concrete and put in some code, in which case if
the subclass does not
rewrite it, and this is known as overriding, the code in
the superclass is
executed. For instance, let's put the following method in
Animal public boolean
isFourLegged() and our implementing code is "return
true;". If we write a Man
class,
subclassing Animal, we will do the
following:
@Override
public boolean isFourLegged() { return false;
}
in this way we save a lot of effort by refactoring a common
property into a common superclass, but can be specific when the subclass
differs. And this brings us to final. Since a class can be subclassed and
public and protected methods overridden, we can change an object's behavior
and this
could raise security issues. For instance, if we can override the
checkPassword method, our subclass could let everyone in by accepting
everything. When we want to make
sure a method cannot be overridden, we
put final in its signature.
Going back to our Animal class for our
simulation, we may wish to have
each animal have a flag for being alive. Inside
the Animal class, we will put the
following instance variable:
private
boolean alive = true;
We will add the following method:
public
boolean isAlive() { return alive; }
and we will add another
method:
public final void dies() {
alive = false;
}
and
now we are sure that none of the subclasses can have something else
happen. I hope this helps.
[ Reply to This | Parent | # ]
|