Chapter 10: Object-Oriented Programming: Polymorphism
•
•
Polymorphism
–
When a program invokes a method through a superclass variable, the correct subclass
version of the method is called, based on the type of the reference stored in the
superclass variable
–
The same method name and signature can cause different actions to occur, depending
on the type of object on which the method is invoked
–
Facilitates adding new classes to a system with minimal modifications to the system’s
code
Polymorphism enables programmers to deal in generalities and let the execution-time
environment handle the specifics. Programmers can command objects to behave in manners
appropriate to those objects, without knowing the types of the objects (as long as the objects
belong to the same inheritance hierarchy).
Allowed assignments between superclasss and subclass variables
1) Assigning a superclass reference to a super class variable
-can refer to the superclass members
2) Assigning a subclass reference to a subclass variable
-can refer to the subclass members as well as the inherited members
3) Assigning a subclass reference to a super class variable
- a subclass is an object of its superclass
-can refer to superclass members
- it is a compile error to use the reference to refer to the subclass only members
Not Allowed assignment
-Attempting to assign a superclass reference to a subclass variable is a compilation error.
-To avoid the error: the superclass reference must be cast to a subclass type explicitly.
104 Abstract Classes and Methods
Classes are: 1. Abstract classes
2. Concrete classes
1. Abstract Class
A class for which the programmer never intends to instantiate objects. Any attempt to instantiate an
object is a compile error.
Abstract superclass: can not be used to instantiate objects. Abstract superclasses are incomplete, so
Subclasses must declare the missing pieces.
public abstract class className
{
}
Notes:
- abstract class normally contains one or more abstract methods.
- a class that contains abstract methods should be declared as an abstract class.
Abstract methods: do not provide implementations
public abstract returntype methodName (parameters);
// should end with
;
Constructors and static methods can not be declared abstract because:
- Constructors are not inherited, so an abstract constructor could never be implemented.
- Subclass can not override static methods so an abstract static method could never be
implemented
2. Concrete classes
- Can be used to instantiate objects
- Provide implementations for every method they declare
- Each concrete subclass of an abstract superclass must provide implementations of
the superclass's abstract methods
- Failure to implement a superclass's abstract methods in a subclass is a
compilation error unless the subclass is also declared abstract
105 /////////Polymorphism ///////////////
Example:
public abstract class Animal{
Animal
public abstract void move();
}
Frog
Bird
Fish
Dove
// Frog class is concrete
public class Frog extends Animal{
public void move( ){
System.out.println("jump");
}
}
//Bird class is abstract
public class abstract Bird extends Animal{
int x,y;
}
//Fish class is concrete
public class Fish extends Animal{
public void move( ){
System.out.println("swim");
}
}
//Dove class is concrete
public class Dove extends Bird{
public void move( ){
System.out.println("fly");
}
}
106