110 likes | 372 Views
Polymorphism. S420. Polymorphism. Polymorphism the ability for a variable (of a superclass type) to contain different objects of a subclass type at different points in time results in different functionality being executed for the same method call
E N D
Polymorphism S420
Polymorphism • Polymorphism • the ability for a variable (of a superclass type) to contain different objects of a subclass type at different points in time • results in different functionality being executed for the same method call • allows for run-time (dynamic) instead of compile-time (static) binding
Polymorphism (contd.) someanimal = somecat; someanimal.display(); someanimal = somebird; someanimal.display(); • display is a method of Animal, Cat and Bird calls display() in Cat calls display() in Bird
Polymorphism (contd.) • big deal!? Animal someanimal[0] = new Cat(“Siamese”, 15, “Grey”, “Raju”, “White”); Animal someanimal[1] = new Bird(“Eagle”, 100, “White”, 15.5, 70.76); ….. for (int j =0; j<200; j++) someanimal[j].display(); // I don’t have to worry if it is a Cat or a Bird
Polymorphism (contd.) • polymorphism vs. overridding vs. overloading • Can you tell the differences of these concepts?
Casting • Casting Upwards • Objects once created always know their type (class) • You can assign objects of a subclass to a superclass object without using the cast operator (implicit casting) someanimal = somecat; • The individual objects still know how to perform their behavior someanimal.display();
Casting (contd.) • Casting Downwards • have to use use an explicit cast Cat somecat = someanimal; //syntax error!! Cat somecat = (Cat) someanimal; • The object itself is NOT changed or converted. • Casting is telling the compiler to ignore the "type mismatch." • Run-time error could occur. Can't assign a super class to a sub class
Casting (contd.) • Casting Downwards • have to use use an explicit cast • You need to cast downwards to use methods defined only in the derived class type Animal someanimal = new Cat(“Siamese”, 15, “Grey”, “Raju”, “White”); Cat somecat = (Cat) someanimal; • but you can create a run-time error by Animal someanimal = new Bird(“Eagle”, 100, “White”, 15.5, 70.76); Cat somecat = (Cat) someanimal; //Run-time ERROR!!
Casting (contd.) • Casting Downwards Animal someanimal = new Cat(“Siamese”, 15, “Grey”, “Raju”, “White”); someanimal.motion(); // syntax error since motion() is not a method of Animal ((Cat)someanimal).motion(); // OK Cat somecat = (Cat) someanimal; Animal someanimal = new Bird(“Eagle”, 100, “White”, 15.5, 70.76); Cat somecat = (Cat) someanimal; /* The compiler trust you that it will be a Cat. But you could make a mistake and create a run-time ERROR!! */ ((Cat)someanimal).motion(); // Run-Time error, too!!!
Casting (contd.) • Casting Downwards (cont.) • Casting downwards to the wrong object is illegal • use instanceof to check the class if (someanimal instanceof Cat) Cat somecat = (Cat) someanimal;
Casting (contd.) • Casting Downwards (cont.) for (int j =0; j< nOfAnimals; j++) if (someanimal[j] instance of Cat) someanimal[j].motion();// syntax error!!!! for (int j =0; j< nOfAnimals; j++) if (someanimal[j] instanceof Cat) { Cat somecat = (Cat) someanimal[j]; somecat.motion(); } • or for (int j =0; j< nOfAnimals; j++) if (someanimal[j] instance of Cat) ((Cat)someanimal[j]).motion();