specialization and inheritance

12
Specialization and Inheritance Chapter 8

Upload: ryder-lindsay

Post on 01-Jan-2016

20 views

Category:

Documents


1 download

DESCRIPTION

Chapter 8. Specialization and Inheritance. 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. Inheritance. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Specialization and Inheritance

Specialization and Inheritance

Chapter 8

Page 2: Specialization and Inheritance

8

SpecializationSpecialized classes inherit the properties and methods of the parent or base class.

A dog is a mammalHairLive birth

A car is a vehicleWheelsEngineTransports people

A Button is a Control

Page 3: Specialization and Inheritance

8

InheritanceSpecialization 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)

Page 4: Specialization and Inheritance

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

Page 5: Specialization and Inheritance

8

Protected Accessprivate fields are inaccessible to outside classes (including derived classes).

protected fields are accessible to derived classes, but not to outside classes.

Page 6: Specialization and Inheritance

8

Using superThe super keyword invokes the base class’s constructor

Must be called from constructor of derived class

Must be first statement within constructor

Page 7: Specialization and Inheritance

8

More Rules for Using superCall 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

Page 8: Specialization and Inheritance

8

PolymorphismAn 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.

Page 9: Specialization and Inheritance

8

Object PolymorphismA 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

Page 10: Specialization and Inheritance

8

Factoring to the Base ClassFactor 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

Page 11: Specialization and Inheritance

8

Abstract Base ClassesThe 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

Page 12: Specialization and Inheritance

8

Abstract MethodsAbstract methods are not implemented.

Derived classes must provide method implementation.