C 8 HAPTER Serious Polymorphism

advertisement
CHAPTER 8
Serious Polymorphism
OVERVIEW
When to not instantiate
 Abstract Classes
 Abstract Methods
 Class Object
 Casting and Object reference
 Multiple Inheritance Problem
 Interfaces

WHEN TO NOT INSTANTIATE
Sometimes a superclass is so generic as to be
pointless to instantiate
 This is not bad because the framework for the
sub classes is still created
 For example, creating a new object of type animal
is pointless if in ALL other subclasses all
properties are overridden

ABSTRACT CLASSES

A class can be protected from instantiation by the
abstract command

This prevents a new object instance to be created.

You can still make a reference variable however
ABSTRACT METHODS
Abstract classes can still have inheritable
methods
 To make sure that subclasses must write their
own methods, individual methods can be made
abstract


This can only be done in abstract classes
CLASS OBJECT
There is the mother of all superclasses, class
Object
 Every class by default inherits directly or
indirectly from class Object
 Some Object methods:

equals()
 getClass()
 hashCode()
 toString()

CASTING AND OBJECT REFERENCE
MULTIPLE INHERITANCE PROBLEM
INTERFACES
Download