A variable can also be polymorphic , ie change its form or class of object reference during the execution of a program. The polymorphism of a variable in Java is limited so that a variable reference to an object can only specialize, just as in real life.
Example polymorphism method sings and the variable foo
class Person {
private String name;
protected String cancion;
public void canta(){System.out.println("la la la");}
} //End Persona
class Ladron extends Persona {
Private int earnings;
sings public void () {System.out.println ("I have not been");}
robs public void (int n) {earnings = earnings + n;}
} / / End Ladron
Judge class extends Person {
Public void sing () {System.out.println (" I am the law");}
} / / End Judge
Poli5 {public class
/ / example of polymorphism sings message
public static void main (String args [])
{
fulanito Person = new Person ();
= new little thief Thief Thief ();
Judge Judge juececito = new ();
fulanito.canta () ; / / sing a person
fulanito = Little thief;
fulanito.canta (); / / sing a thief
fulanito.roba (100);
/ / error compilation: foo is a Person. Solution?
foo = juececito;
fulanito.canta (); / / sing a judge
}
} / / End Poli5
Restriction polymorphism Java syntax: change to subclasses or specialization.
static type Verification: compile time.
0 comments:
Post a Comment