Enviar búsqueda
Cargar
11 polymorphism
•
Descargar como PPTX, PDF
•
0 recomendaciones
•
323 vistas
P
Program in Interdisciplinary Computing
Seguir
Denunciar
Compartir
Denunciar
Compartir
1 de 15
Descargar ahora
Recomendados
In defense of pythagoras
In defense of pythagoras
guest9e9e72
Process Chapter Notes
Process Chapter Notes
Program in Interdisciplinary Computing
06b extra methods
06b extra methods
Program in Interdisciplinary Computing
CH 9: Editorial Style Notes
CH 9: Editorial Style Notes
Program in Interdisciplinary Computing
Chapter.07
Chapter.07
Program in Interdisciplinary Computing
3 Information Architecture
3 Information Architecture
Program in Interdisciplinary Computing
11 interfaces
11 interfaces
Program in Interdisciplinary Computing
01 intro to using java
01 intro to using java
Program in Interdisciplinary Computing
Recomendados
In defense of pythagoras
In defense of pythagoras
guest9e9e72
Process Chapter Notes
Process Chapter Notes
Program in Interdisciplinary Computing
06b extra methods
06b extra methods
Program in Interdisciplinary Computing
CH 9: Editorial Style Notes
CH 9: Editorial Style Notes
Program in Interdisciplinary Computing
Chapter.07
Chapter.07
Program in Interdisciplinary Computing
3 Information Architecture
3 Information Architecture
Program in Interdisciplinary Computing
11 interfaces
11 interfaces
Program in Interdisciplinary Computing
01 intro to using java
01 intro to using java
Program in Interdisciplinary Computing
WSG CH08 - Typography
WSG CH08 - Typography
Program in Interdisciplinary Computing
5 wsg site-structure
5 wsg site-structure
Program in Interdisciplinary Computing
08 boolean expressions
08 boolean expressions
Program in Interdisciplinary Computing
CH11 Graphics Notes
CH11 Graphics Notes
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
14b exceptions
14b exceptions
Program in Interdisciplinary Computing
12 constructors
12 constructors
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
CH 12 Multimedia Notes
CH 12 Multimedia Notes
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
Phpmysqlcoding
Phpmysqlcoding
Program in Interdisciplinary Computing
Chapter.03
Chapter.03
Program in Interdisciplinary Computing
Database basics
Database basics
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
01 intro tousingjava
01 intro tousingjava
Program in Interdisciplinary Computing
Web architecture v3
Web architecture v3
Program in Interdisciplinary Computing
Xhtml
Xhtml
Program in Interdisciplinary Computing
Webdev
Webdev
Program in Interdisciplinary Computing
Web architecture
Web architecture
Program in Interdisciplinary Computing
Sdlc
Sdlc
Program in Interdisciplinary Computing
Más contenido relacionado
Destacado
WSG CH08 - Typography
WSG CH08 - Typography
Program in Interdisciplinary Computing
5 wsg site-structure
5 wsg site-structure
Program in Interdisciplinary Computing
08 boolean expressions
08 boolean expressions
Program in Interdisciplinary Computing
CH11 Graphics Notes
CH11 Graphics Notes
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
14b exceptions
14b exceptions
Program in Interdisciplinary Computing
12 constructors
12 constructors
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
CH 12 Multimedia Notes
CH 12 Multimedia Notes
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
Phpmysqlcoding
Phpmysqlcoding
Program in Interdisciplinary Computing
Chapter.03
Chapter.03
Program in Interdisciplinary Computing
Destacado
(12)
WSG CH08 - Typography
WSG CH08 - Typography
5 wsg site-structure
5 wsg site-structure
08 boolean expressions
08 boolean expressions
CH11 Graphics Notes
CH11 Graphics Notes
Chapter.08
Chapter.08
14b exceptions
14b exceptions
12 constructors
12 constructors
Chapter.08
Chapter.08
CH 12 Multimedia Notes
CH 12 Multimedia Notes
CGS2835 HTML5
CGS2835 HTML5
Phpmysqlcoding
Phpmysqlcoding
Chapter.03
Chapter.03
Más de Program in Interdisciplinary Computing
Database basics
Database basics
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
01 intro tousingjava
01 intro tousingjava
Program in Interdisciplinary Computing
Web architecture v3
Web architecture v3
Program in Interdisciplinary Computing
Xhtml
Xhtml
Program in Interdisciplinary Computing
Webdev
Webdev
Program in Interdisciplinary Computing
Web architecture
Web architecture
Program in Interdisciplinary Computing
Sdlc
Sdlc
Program in Interdisciplinary Computing
Mysocial
Mysocial
Program in Interdisciplinary Computing
Javascript
Javascript
Program in Interdisciplinary Computing
Javascript
Javascript
Program in Interdisciplinary Computing
Html5
Html5
Program in Interdisciplinary Computing
Frameworks
Frameworks
Program in Interdisciplinary Computing
Drupal
Drupal
Program in Interdisciplinary Computing
Database
Database
Program in Interdisciplinary Computing
Javascript2
Javascript2
Program in Interdisciplinary Computing
12 abstract classes
12 abstract classes
Program in Interdisciplinary Computing
13 interfaces
13 interfaces
Program in Interdisciplinary Computing
Más de Program in Interdisciplinary Computing
(20)
Database basics
Database basics
Mysocial databasequeries
Mysocial databasequeries
Mysocial databasequeries
Mysocial databasequeries
CGS2835 HTML5
CGS2835 HTML5
01 intro tousingjava
01 intro tousingjava
Web architecture v3
Web architecture v3
Xhtml
Xhtml
Webdev
Webdev
Web architecture
Web architecture
Sdlc
Sdlc
Mysocial
Mysocial
Javascript
Javascript
Javascript
Javascript
Html5
Html5
Frameworks
Frameworks
Drupal
Drupal
Database
Database
Javascript2
Javascript2
12 abstract classes
12 abstract classes
13 interfaces
13 interfaces
11 polymorphism
1.
Using IS-A and
HAS-A LIS4930 © PIC When you want to know if one thing should extend another; apply the IS-A test. Triangle IS-A Shape. YES Cat IS-A Feline. YES Bathroom has a reference to a Tub, but Bathroom does not extend Tub and vice-versa. Surgeon IS-A Doctor. YES Tub IS-A Bathroom. NO Bathroom HAS-A Tub. Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable.
2.
Wait! There’s More!
LIS4930 © PIC If class B extends A, class B IS-A class A. This is true anywhere in the inheritance tree. If class C extends class B, class C passes the IS-A test for both B and A. Canine extends Animal Wolf extends Canine Wolf extends Animal Canine IS-A Animal Wolf IS-A Canine Wolf IS-A Animal Keep in mind that the inheritance IS-A relationship works in only ONE direction! Animal makeNoise() eat() sleep() roam() Canine roam() Wolf makeNoise() eat()
3.
Test Yourself! LIS4930
© PIC Oven extends Kitchen Guitar extends Instrument Person extends Employee Ferrari extends Engine FriedEgg extends Food Beagle extends Pet Container extends Jar Metal extends Titanium Beverage extends Coke
4.
Access Level Control
LIS4930 © PIC A subclass inherits all public instance variables and methods of the superclass, but does not inherit the private instance variables and methods of the superclass. public members are inherited private members are not inherited Mammal private weight public color private eat() private roam() public sleep() Elephant makeNoise()
5.
Polymorphism! What is
that? LIS4930 © PIC I think this is best explained with an example. Let’s step back and look at the way we normally declare a reference and create an object… myDog Declare a reference variable Dog myDog 1 Dog
6.
Polymorphism! What is
that? LIS4930 © PIC Dog Create an object Dog myDog = new Dog() 2 Dog object
7.
Polymorphism! What is
that? LIS4930 © PIC myDog Dog Link the object and the reference Dog myDog= new Dog() 3 Dog Dog object
8.
Polymorphism! What is
that? LIS4930 © PIC myDog Dog Dog Dog object The important point is that the reference type AND the object type are the same.
9.
Polymorphism! What is
that? LIS4930 © PIC myDog Dog Animal Dog object But with polymorphism, the reference and the object can be different. Animal myDog= new Dog()
10.
Polymorphic Example LIS4930
© PIC Animal[ ] animals = new Animal[5]; animals[0] = new Dog(); animals[1] = new Cat(); animals[2] = new Wolf(); animals[3] = new Hippo(); animals[4] = new Lion(); for(inti = 0; i < animals.length; i++){ animals[i].eat(); animals[i].roam(); }
11.
You can have
polymorphic arguments and return types LIS4930 © PIC class Vet { public void giveShot(Animal a) { a.makeNoise(); } } a class PetOwner { public void start() { Vet v = new Vet(); Dog d = new Dog(); Hippo h = new Hippo(); v.giveShot(d); v.giveShot(h); } } Animal
12.
Taking Advantage of
Polymorphism LIS4930 © PIC With polymorphism, you can write code that doesn’t have to change when you introduce new subclass types in the program. That means if others want to take advantage of your Vet class, all they have to do is make sure their new Animal types extend class Animal.
13.
Keeping the Contract:
Rules for Overriding LIS4930 © PIC Arguments must be the same, and return types must be compatible 1 This is NOT an override. Can’t change the arguments in an overriding method! This is actually a legal overLOAD, but not an overRIDE. Appliance booleanturnOn() Toaster booleanturnOn(int level)
14.
Keeping the Contract:
Rules for Overriding LIS4930 © PIC The method can’t be less accessible. 2 NOT LEGAL! It’s not a legal override because we restricted the access level. Nor is it a legal overLOAD because we didn’t change arguments. Appliance public booleanturnOn() Toaster private booleanturnOn()
15.
Overloading A Method
LIS4930 © PIC Method overloading is nothing more than having two methods with the same name but different argument lists. Period. It has nothing to do with inheritance and polymorphism. An overloaded method is NOT the same as an overridden method. 1 2 3 The return types can be different public class Overloads { String uniqueID; public intaddNums(int a, intb) { return a + b; } public double addNums(double a, double b) { return a + b; } public void setUniqueID(String ID){ uniqueID = theID; } private void setUniqueID(intssNumber){ String numString = “” + ssNumber; setUniqueID(numString); } } You can’t change ONLY the return type You can vary the access levels in any direction
Descargar ahora