Chapter 08

204 views

Published on

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
204
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 08

  1. 1. Specialization and Inheritance Chapter 8
  2. 2. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is a mammal Hair Live birth A car is a vehicle Wheels Engine Transports people A Button is a Control
  3. 3. 8 Inheritance Specialization is implemented in Java through inheritance. The extends keyword is used to implement this relationship between classes. class Dog extends (specializes) Mammal Mammal is the base class (superclass) Dog is the derived class (subclass)
  4. 4. 8 Code Reuse with Inheritance Inheritance permits easy reuse of existing code. A DeadBall class can be derived from a Ball class. Do not have to recode Ball characteristics in DeadBall class
  5. 5. 8 Protected Access private fields are inaccessible to outside classes (including derived classes). protected fields are accessible to derived classes, but not to outside classes.
  6. 6. 8 Using super The super keyword invokes the base class’s constructor Must be called from constructor of derived class Must be first statement within constructor
  7. 7. 8 More Rules forUsing super Call must match the signature of a valid signature in the base class Implicitly called in the constructor if omitted, so the base class must have a default constructor If derived class does not define a constructor, the compiler provides one and calls super automatically
  8. 8. 8 Polymorphism An object can take many forms. Method overloading is one type of polymorphism. Object polymorphism treats specialized objects as if they are instances of a more general type.
  9. 9. 8 Object Polymorphism A method expecting a Car object can only accept Car objects A method expecting Vehicle objects can accept any object derived from the Vehicle class Car Truck Bus
  10. 10. 8 Factoring to the Base Class Factor common characteristics of multiple classes up into a base class. HWall and VWall classes have similar characteristics and methods. Factor commonalities to a Wall class Override some methods to specialize the HWall and VWall classes
  11. 11. 8 Abstract Base Classes The classes describe what all derived classes have in common. Not instantiated (causes an exception) Use the abstract keyword when defining abstract base classes and methods
  12. 12. 8 Abstract Methods Abstract methods are not implemented. Derived classes must provide method implementation.

×