06 - Abstract Class and Interface-MJS
06 - Abstract Class and Interface-MJS
}
When to use/create Abstract class
The implementation of some methods are too
vary for each child
No concrete/actual object for the parent class
– No such object exists
– Parent doesn’t need to be instantiated
When class A
implements Interface I,
Trooper
the diagram was
denoted as
+ shootWeapon()
Interface
Like an abstract class, interface cannot be
instantiated
A class that implements interface(s) must
implements all abstract methods
An abstract class can also implements interface(s)
An abstract class that implements interface(s)
may not implements all abstract methods
Interface
Practical use of interfaces enables the separation
of the interface and implementation of an object
– The interface is provided by the interface definition
– The implementation is provided by the classes which
implement the interface methods
Interface
Interface usually named "…… - able"
(but not always)
– Serializable
– Runnable
– Cloneable
Mammal Birds
- Fur/hair - Wings
- mammaryGland - beak
Bat Ostrich
Example: Animal Hierarchy
Animal whales and penguins
are aquatic animals
and can swim, but
Mammal Birds
bats, tigers, eagles and
ostriches can not
<<interface>> <<interface>>
Aquatic Carnivorous
+ swim() + eatMeat()
+ dive()
Example: Animal Hierarchy
Animal
One class can only have
one parent
One class may implement
Mammal Birds
many interface
Bat Ostrich
<<interface>>
Taxable
- taxRate = 0.06
+ calculateTax()
Question?
THANK YOU