Terms and Rules II Professor Evan Korth New York University (All rights reserved)

Download Report

Transcript Terms and Rules II Professor Evan Korth New York University (All rights reserved)

Terms and Rules II
Professor Evan Korth
New York University
(All rights reserved)
inheritance
• In object orientated programming, inheritance
refers to the process by which one class
automatically gets the members of another class
without having to re-declare the members in the
new class.
• Thus inheritance, fosters code reuse on a large
scale.
• It also requires us to think about which members
are shared between the classes we design.
• The keyword extends is used in Java for
inheritance.
Inheritance (cont)
• A class that extends another class is said to be a
subclass. It is also known as a child class or a
derived class.
• The class it extends is called a superclass.
Sometimes the superclass is called a base class or
a parent class.
• So a subclass inherits the non-private members of
it’s superclass.
• Typically, the subclass will also add members
giving it more functionality and making it more
specific than it’s superclass.
• A superclass can also modify its method members.
Keyword super
• The keyword super can be used in a subclass to
refer to members of the subclass’ superclass. It is
used in two ways:
– To invoke a constructor of the superclass (more about
this on the next slide). For example (note: the keyword
new is not used):
• super ();
• super (params);
– To call a superclass’ method (it is only necessary to do
so when you override (more on overriding soon too) the
method). For example:
• super.superclassMethodName();
• super.superclassMethodName(params);
Inheritance and constructors
• Constructors do not get inherited to a subclass.
Instead, the subclass’ constructor automatically
calls it’s superclass’ default constructor before it
executes any of the code in it’s own constructor.
• If you do not want to use the default constructor,
you must explicitly invoke one of the superclass’
other constructors with the keyword super.
– If you do this, the call to the superclass’ constructor
must be the first line of code in the subclass’ constructor.
• If the superclass has no default constructor, the
first line of any subclass constructor MUST
explicitly invoke another superclass constructor.
Overriding methods
• Often a subclass does not want to have the same exact
method implementations as it’s superclass. In this
case, the subclass can override the methods that it
wants to change.
• To override a method, you must re-declare the method
in the subclass. The new method must have the exact
same signature as the superclass’ method you are
overriding.
• Only non-private methods can be overridden because
private methods are not visible in the subclass.
• Note: You cannot override static methods.
• Note: You cannot override fields.
• In the above two cases, when you attempt to override
them, you are really hiding them.
Object class
• In Java, all classes are derived from other
classes except the object class which is the
top of Java’s class hierarchy.
• Therefore, if a new class does not explicitly
extend another class, it implicitly extends
the object class.
• Several of the methods provided in the
object class are provided with the intention
that they will be overridden.
Object class: equals method
public boolean equals (Object object)
• object’s equals method returns true if the
objects are the same object (ie the two
variables refer to the same position in memory)
• Since you can already check for that condition
with the == operator, you are meant to override
the equals method with one that will check to
see if the objects have the same fields.
Object class: toString method
public String toString ()
• object’s toString method returns the
name of the class of the object plus an @
sign and a number representing the object.
• You should override toString to return a
string that more closely represents the
object.
• Whenever you print an object, the result of
method toString is what will be printed.
Overload vs override
• Overloading a method refers to having two
methods which share the same name but
have different signatures.
• Overriding a method refers to having a new
implementation of a method with the same
signature in a subclass.
Visibility modifiers for data and
methods (review)
• Public means the data / method is visible to
everything.
• No modifier (default) means the data /
method is visible in the package in which it
is declared.
• Private means the data / method is visible
only within the class in which it is defined.
– Trying to access private data from another class
will result in a compile time error.
Visibility modifiers for data and
methods (cont)
• Protected means the data / method is
visible:
– In any class in the same package
– In any of the class’ subclasses (even if the
subclasses are in different packages)
– Note: When a protected member is overridden,
it’s visibility can be changed to public to allow
greater access but not less access (less access is
called weaker access privileges in Java
terminology).
Visibility modifiers for data and
methods (cont)
Weakest
access
privieges
private – only seen in class
(UML -)
default (no visibility modifier) – private
access plus classes in the package
(UML none)
protected – default access plus subclasses
(UML #)
Strongest
access
privieges
public – protected access plus everything else
(UML +)
final modifier
• The many faces of the final modifier
– When applied to a data field, final means the
field is a constant.
– When applied to a class, final means the
class cannot be extended.
– When applied to a method, the method cannot
be overridden.
– When applied to a variable that is local to a
method, final means the variable is a
constant.
Keyword abstract
• An abstract class is a class that cannot be
instantiated.
• An abstract method is a method signature without
an implementation.
• Any class that contains an abstract method must
itself be abstract. However, an abstract class can
contain both abstract and concrete methods.
• Any non-abstract sub-class of an abstract class
must provide an implementation for every abstract
method of it’s superclass.
• (UML italics)
NOTE
An abstract method cannot be
contained in a nonabstract
class. If a subclass of an
abstract superclass does not
implement all the abstract
methods, the subclass must
be declared abstract. In
other words, in a
nonabstract subclass
extended from an abstract
NOTE
A subclass can be abstract
even if its superclass is
concrete. For example, in
the book, the Object class
is concrete, but its
subclasses, such as
GeometricObject, may be
abstract.
NOTE
You cannot create an
instance from an abstract
class using the new
operator, but an abstract
class can be used as a data
type. Therefore, the
following statement, which
creates an array whose
elements are of
GeometricObject type which
is an abstract class, is
polymorphism
• An object of a subclass can
be used by any code designed
to work with an object of
its superclass. This feature
is known as polymorphism
(from a Greek word meaning
“many forms”).
Dynamic binding
• The Java Virtual Machine can figure out which
(possibly overloaded) method to use at run time.
• Dynamic binding allows us to use variables of
superclasses which refer to objects of its various
subclasses. When we use such a superclass
variable in a method call, the JVM will choose the
correct implementation of the method to use.
– It starts with the “sub-most” class and works its way up
until a method with the correct signature is found.
Choosing the method
• The compiler is responsible for choosing
which method signature will be used for a
given method call. It must make sure the
method call’s parameter list matches a
parameter list of one of the method
declarations.
• The JVM is responsible for binding the
method call to the proper implementation of
the given method signature.
Casting objects
• Classes variables can refer to objects of related
classes as long the object has an “is a” relationship
to the class.
• Since subclasses have an ‘is-a’ relationship with
their superclass, we can always point a superclass
variable to a subclass object. In such case a
casting operator is not necessary. You implicitly
cast the object to it’s superclass just by using the
gets operator in an assignment statement. For
example:
Superclass sup = new Subclass();
or
Superclass sup = subclassVar;
Casting objects (cont)
• On the other hand, an object referenced by a superclass
variable may or may not be a subclass object.
• To attempt an assignment such as:
Subclass sub = superclassVar;
You must explicitly cast the superclass object to the
subclass type. For example:
Subclass sub = (Subclass) supclVar;
• If the variable supclVar in the example above is not an
instance of Subclass, a runtime error will occur.
Casting objects (cont)
• In order to help prevent the type of runtime error described in the
last slide, Java has the keyword instanceof, which is used to
test a variable to see if it is an instance of a class. For example:
(supclVar instanceof Subclass)
will evaluate to true if and only if supclVar is an instance of
class Subclass.
• So we can test a variable to see if it is an instance of a class before
we make an explicit cast. For example:
if (supclVar instanceof Subclass)
Subclass sub = (Subclass) supclVar;
• Will never cause a runtime error because the assignment will only
happen in the event that supclVar is an instance of Subclass.
Interfaces
• In Java, only single inheritance is permitted. However, Java
provides a construct called an interface which can be
implemented by a class.
• Interfaces are similar to abstract classes (we will compare the
two soon).
• A class can implement any number of interfaces. In effect
using interfaces gives us the benefit of multiple inheritance
without many of it’s problems.
• Interfaces are complied into bytecode just like classes.
• Interfaces cannot be instantiated.
• Can use interface as a data type for variables.
• Can also use an interface as the result of a cast operation.
• Interfaces can contain only abstract methods and constants.
Interfaces (cont)
• An interface is created with the following
syntax:
modifier interface interfaceID
{
//constants/method signatures
}
• public class Point extends Object
implements Shape, Comparable {
•
Interfaces (cont)
• An interface can extend other interfaces with the
following syntax:
modifier interface interfaceID
extends comma-delimited-list-ofinterfaces
{
//constants/method signatures
}
• Obviously, any class which implements a “subinterface” will have to implement each of it’s
“super-interfaces”
Interface vs. abstract class
Interface
Abstract
class
Fields
Only constants
Constants and
variable data
Methods
No
implementation
allowed (no
abstract
modifier
necessary)
Abstract or
concrete
Interface vs. abstract class (cont)
Interface
Inheritance
Abstract
class
A subclass can A subclass can
implement many inherit only one
interfaces
class
Can extend
numerous
interfaces
Cannot extend a
class
Can implement
numerous
interfaces
Can extend one
class
Interface vs. abstract class (cont)
Interface
Abstract
class
Root
none
object
names
Adjective or
Nouns
Nouns