1 / 11

Polymorphism

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

rashad
Télécharger la présentation

Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism S420

  2. 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

  3. 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

  4. 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

  5. Polymorphism (contd.) • polymorphism vs. overridding vs. overloading • Can you tell the differences of these concepts?

  6. 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();

  7. 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

  8. 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!!

  9. 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!!!

  10. 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;

  11. 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();

More Related