SE 450 Fall 2001/2002
Week 3 Lecture Notes
Method Overloading
- Each method has a signature:
its name together with the number and types of its parameters
- Return type, parameter names and final designation do not have an affect
on signature
-
Order of parameters is important in signature
Two methods can have the same name if they have different signatures.
They are overloaded.
Calls to overloaded methods are resolved at compile time
NO operator overloading in Java
Example on p.119 of Jia
Extending Classes (Inheritance)
-
Inheritance defines a relationship among classes.
- All reference type inherit from java.lang.Object which give them some base
functionality (toString(), getClass(), etc)
- All subclasses inherit public, protected and "package" private fields
and methods.
Inheritance and Constructors
- Initialzation of subclasses has two phases that occur in the following order:
- initialization of super class fields
- initialization of its own fields
- If you don't provide a constructor then the default constructor is proveded.
- This constructor simply calls the default constructor of the super class.
- Example: a colored point (p.123 in Jia)
- Constructor gotchas:
-
no default constructor in super class but other constructors exist
-
no default constructor in sub class
Polymorphism
- All subclasses are specializations of the super class. They contain
the base functionality plus possibly add their own.
- You can assign instances of subtypes to a variable that has type of the
super class (polymorphic assignment). The instances are automatically
upcasted to the super class type. This happens automatically(implicit).
- Downcasting is need to assign a super class instance to a variable of
subclass type. This must be done by user(explicit).
- Allows for run time binding of method calls on object references.
Overriding Methods
- When you implement a method in a sub class that has the same name, signature,
and return type of a method in the superclass.
- Done to implement the same functionality but different implementations within
the object hierarchy.
- Final methods in the super class can not be overridden in a sub class.
Interfaces
ClassModifiers interface InterfaceName
[extends Interfaces]{
list of method or variable declarations
(methods have no implementation and fields must be static and final)
}
- Interfaces declare features but no implementation (contract with the user).
- Java's means of providing multiple inheritance.
- Implementing multiple interfaces allow a class to fill different roles in
different situations.
- Classes that implement an interface provide implemetation for all
methods defined and inherit all variables (which are just constants).
- Rules of polymorphic assignment apply to the interface/implementor
relationship in the same way that they do to the superclass/subclass relationship.