Title: Realization EEE321'07
1RealizationEEE321.07
Royal Military College of Canada Electrical and
Computer Engineering
Refs Ambler Ch 5Booch Ch 1
- Maj JW Paul
- Jeff.Paul_at_rmc.ca
- 1-613-541-6000 x6656
2There are 10 kinds of people in the world.Those
who understand binary,
and those who dont...
3Review
- What is Polymorphism?
- Greek appearing in many forms
- What is overloading?
- meaning based on context
- method signature
- operators
- What is overriding?
- sub-type conversion
4Overriding
- Using a method with the same signature (which
means?) but in a different way in a different
class - The distinction is based on how the object is
called - Dynamic binding (at runtime)
5Example of Overriding
6Polymorphism
7An example
square
8Restriction in Java
- If a method overrides a superclass method, it is
not permitted to have a lower access level in the
subclass than it does in the superclass JLS
8.4.6.3
9Parametric Polymorphism
- Depends on type of the call parameter at the time
of the execution of the program. - A very powerful feature
- Does not exist in Java
- C has templates
10Power of Polymorphism
11Polymorphic
12Good Programming
- Minimize access to classes and methods
- Priority
- Private
- Package (aka Friendly)
- Protected
- Public
- Public access should be very rare
13Restriction in Java
- If a method overrides a superclass method, it is
not permitted to have a lower access level in the
subclass than it does in the superclass JLS
8.4.6.3
14Abstract Classes
An Abstract Class can never be implemented (no
objects will be instantiated directly from this
class)
15Abstract vs Concrete Class
- An abstract class can have methods and attributes
- If an abstract class has only methods, what is
it? - An interface
16Interface
- An interface is a variation of an abstract class
- all the methods are abstract (what does this
mean) - defines only static final member variables
- We do not inherit from interfaces, we realize
them - Useful as it allows Java programmer to simulate
multiple inheritance
17Uses of Interfaces
- Revealing an objects programming interface
without revealing actual body of the class. - Capturing similarities between unrelated classes
without forcing a class relationship - Describing "function-like" objects that can be
passed as parameters to methods invoked on other
objects - We say that the the implementation of a class is
hidden behind the interface
18Interface in UML
19Interface in UML (alternate)
20Interface in UML
21Implementation in Java
22Another Example
23Inheritance vs Realization
24Abstract Class vs Interface
- Abstract Class
- only instantiated by a subclass
- may contain implemented methods
- may contain abstract methods.
- may contain attributes.
- Interface
- implemented by another class
- may not contain implemented methods
- may contain method signatures
- may only contain constants (static, final)
25Factory Method
26Pattern Factory Method
- Factory Method gives the responsibility of
creating instances to the sub class - Use when
- The class does not know in advace what type of
object it must create - The class wants the sub-class to specify what
type of object needs to be created - Allows one to decouple the product and the
producer
27Factory Method General Structure
Creator
Product
factoryMethod()
...
anOperation()
product factoryMethod()
...
ConcreteCreator
constructor
ConcreteProduct
...
factoryMethod()
return new ConcreteProduct
...
28Factory Method example
create
29Factory Method Java
Remarquer le type de retour est Document et non
ascii.
30Good Programming
- Use Factory Method instead of the regular
constructor Effective Java - item 1.
31Review
- Why is Polymorphism Important?