Java TOPIC – INHERITANCE & POLYMORPHISM II LESSON NOTE DATA ENCAPSULATION WITH
INHERITENCE (INTRODUCING PROTECTED) An instance
variable of the superclass is automatically an instance variable of the
subclass. It is an error to declare it
again in the subclass. However, we
likely want our instance variables to be accessible from the subclass but nor
from other classes in order to preserve data encapsulation. This can be done by using protected instead
of either public or private. A protected instance
variable is accessible by subclasses but not by other classes. So, a protected instance variable is like a
public instance variable for subclasses but like a private instance variable
for other classes. OVERRIDING METHODS A subclass
may declare a new version of any public/protected superclass method to
provide additional/different functionality.
This is called method
overriding. In doing so, a
subclass method can call the superclass version of the method by using the
syntax: super.methodName(argList) Public or
protected superclass methods that are not overridden are automatically
available to the subclass without the super
keyword. Note that a
common error for programmers is to intend to override a method but to
accidentally make is slightly different (by giving either a different name or
a different argument list). You can
tell the compiler to watch for this by adding annotation @Override on the
line about the method. For example,
we have added the annotation for the method overridingMethod
below. This tells the compiler to make
sure that this method exists in the superclass and is in fact overriding
another method. @Override public int overridingMethod(int a) { //implementation
not shown }
POLYMORPHISM
Consider the
example above with the superclass Person and the subclass Student. Java allows us to do this: Person p = new Student("Pat
Campo", 22, 123456, "LCS"); This is
polymorphism. Polymorphism allows a
superclass object reference to be set to an object of a subclass. It is allowing the reference to take
different forms (poly morphing). We can
therefore create an array of Person object that contain Student objects as
well as any other types of Person objects.
Then, we can process them all at once in a loop. One thing,
when we have a superclass reference to a subclass object (like above), we can
only use methods and instance variables from the superclass. We cannot use methods or instance variables
for the subclass.
This is one
exception to the above rule. If a
method in the superclass is overridden in the subclass, then Java will
automatically call the overriding method even on a superclass reference. So, p.toString() would call the toString() method in Student if there was one. THE OBJECT CLASS In Java, all
classes that do not extend another class automatically extend the Object
class. (There is no need to specify
this inheritance.) The object
class is actually the source of the default toString()
method that outputs the strange sequence of hex characters. This also
allows us to create methods that have an Object as parameter. We can then pass any other object to the
function because all classes inherit from Object. |