Interfaces and Polymorphism

Interface

Why is it bad to add new methods to an Interface after it has already been implemented?

 

 

Interfaces allow for encapsulating similar behaviors between unrelated classes.

Can an interface have:

Interface declarations:

Abstract

Interfaces

       Flier b = new Bird();
       Flier s = new SkiJumper();
       Flier f = new Flier(); 

Actual method invoked is defined by the object’s class, at ___________ (dynamic binding)

Polymorphism…

The importance of IS-A

Which of these are OK?

You can convert from a class type to an interface type if the class implements the interface.

 String str = "word"; 
 Comparable p = str; 

You need a cast to convert from an interface type to a class type.

 String str = "word"; 
 Comparable p = str; 
 String t = p; 

You can convert from a class type to an interface type if the class realizes the interface.

 String str = "word"; 
 Comparable p = str; 

You need a cast to convert from an interface type to a class type.

 String str = "word"; 
 Comparable p = str; 
 String t = (String)p; 

The instanceof operator tests whether an object belongs to a particular type.

 Flier aFlier = new SkiJumper("Joe","Smith"); 
 Flier aFlier2 = new Airplane(); 

True or false:

 Polymorphism is NOT Overloading.

Polymorphism