SlideShare a Scribd company logo
Using IS-A and HAS-ALIS4930 © PICWhen you want to know if one thing should extend another; apply the IS-A test.Triangle IS-A Shape.YESCat IS-A Feline.YESBathroom has a reference to a Tub, but Bathroom does not extend  Tub and vice-versa.Surgeon IS-A Doctor.YESTub IS-A Bathroom.NOBathroom HAS-A Tub.Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable.
Wait! There’s More!LIS4930 © PICIf 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 AnimalWolf extends CanineWolf extends AnimalCanine IS-A AnimalWolf IS-A CanineWolf IS-A AnimalKeep in mind that the inheritance IS-A relationship works in only ONE direction!AnimalmakeNoise()eat()sleep()roam()Canineroam()WolfmakeNoise()eat()
Test Yourself!LIS4930 © PICOven extends KitchenGuitar extends InstrumentPerson extends EmployeeFerrari extends EngineFriedEgg extends FoodBeagle extends PetContainer extends JarMetal extends TitaniumBeverage extends Coke
Access Level ControlLIS4930 © PICA 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 inheritedprivate members are not inheritedMammalprivate weightpublic colorprivate eat()private roam()public sleep()ElephantmakeNoise()
Polymorphism! What is that?LIS4930 © PICI 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…myDogDeclare a reference variableDog myDog1Dog
Polymorphism! What is that?LIS4930 © PICDogCreate an objectDog myDog = new Dog()2Dog object
Polymorphism! What is that?LIS4930 © PICmyDogDogLink the object and the referenceDog myDog= new Dog()3DogDog object
Polymorphism! What is that?LIS4930 © PICmyDogDogDogDog objectThe important point is that the reference type AND the object type are the same.
Polymorphism! What is that?LIS4930 © PICmyDogDogAnimalDog objectBut with polymorphism, the reference and the object can be different.Animal myDog= new Dog()
Polymorphic ExampleLIS4930 © 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();	}
You can have polymorphic arguments and return typesLIS4930 © PICclass Vet {	public void giveShot(Animal a) {a.makeNoise();	}}aclass PetOwner {	public void start() {		Vet v = new Vet();		Dog d = new Dog();		Hippo h = new Hippo();v.giveShot(d);v.giveShot(h);	}}Animal
Taking Advantage of PolymorphismLIS4930 © PICWith 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.
Keeping the Contract: Rules for OverridingLIS4930 © PICArguments must be the same, and return types must be compatible1This is NOT an override. Can’t change the arguments in an overriding method!This is actually a legal overLOAD, but not an overRIDE.AppliancebooleanturnOn()ToasterbooleanturnOn(int level)
Keeping the Contract: Rules for OverridingLIS4930 © PICThe method can’t be less accessible.2NOT 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.Appliancepublic booleanturnOn()Toasterprivate booleanturnOn()
Overloading A MethodLIS4930 © PICMethod 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.123The return types can be differentpublic 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 typeYou can vary the access levels in any direction

More Related Content

PPT
In defense of pythagoras
PPT
PPT
CH 9: Editorial Style Notes
PPT
3 Information Architecture
PPTX
Ad

More from Program in Interdisciplinary Computing (20)

Ad

11 polymorphism

  • 1. Using IS-A and HAS-ALIS4930 © PICWhen you want to know if one thing should extend another; apply the IS-A test.Triangle IS-A Shape.YESCat IS-A Feline.YESBathroom has a reference to a Tub, but Bathroom does not extend Tub and vice-versa.Surgeon IS-A Doctor.YESTub IS-A Bathroom.NOBathroom HAS-A Tub.Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable.
  • 2. Wait! There’s More!LIS4930 © PICIf 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 AnimalWolf extends CanineWolf extends AnimalCanine IS-A AnimalWolf IS-A CanineWolf IS-A AnimalKeep in mind that the inheritance IS-A relationship works in only ONE direction!AnimalmakeNoise()eat()sleep()roam()Canineroam()WolfmakeNoise()eat()
  • 3. Test Yourself!LIS4930 © PICOven extends KitchenGuitar extends InstrumentPerson extends EmployeeFerrari extends EngineFriedEgg extends FoodBeagle extends PetContainer extends JarMetal extends TitaniumBeverage extends Coke
  • 4. Access Level ControlLIS4930 © PICA 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 inheritedprivate members are not inheritedMammalprivate weightpublic colorprivate eat()private roam()public sleep()ElephantmakeNoise()
  • 5. Polymorphism! What is that?LIS4930 © PICI 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…myDogDeclare a reference variableDog myDog1Dog
  • 6. Polymorphism! What is that?LIS4930 © PICDogCreate an objectDog myDog = new Dog()2Dog object
  • 7. Polymorphism! What is that?LIS4930 © PICmyDogDogLink the object and the referenceDog myDog= new Dog()3DogDog object
  • 8. Polymorphism! What is that?LIS4930 © PICmyDogDogDogDog objectThe important point is that the reference type AND the object type are the same.
  • 9. Polymorphism! What is that?LIS4930 © PICmyDogDogAnimalDog objectBut with polymorphism, the reference and the object can be different.Animal myDog= new Dog()
  • 10. Polymorphic ExampleLIS4930 © 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 typesLIS4930 © PICclass Vet { public void giveShot(Animal a) {a.makeNoise(); }}aclass 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 PolymorphismLIS4930 © PICWith 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 OverridingLIS4930 © PICArguments must be the same, and return types must be compatible1This is NOT an override. Can’t change the arguments in an overriding method!This is actually a legal overLOAD, but not an overRIDE.AppliancebooleanturnOn()ToasterbooleanturnOn(int level)
  • 14. Keeping the Contract: Rules for OverridingLIS4930 © PICThe method can’t be less accessible.2NOT 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.Appliancepublic booleanturnOn()Toasterprivate booleanturnOn()
  • 15. Overloading A MethodLIS4930 © PICMethod 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.123The return types can be differentpublic 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 typeYou can vary the access levels in any direction