1 / 16

Chapter 7

Chapter 7. Better Living in Objectville. Overview. Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods IS-A and HAS-A What is inherited from a superclass What does inheritance really help with Polymorphism Rules for Overriding

zaria
Download Presentation

Chapter 7

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. Chapter 7 Better Living in Objectville

  2. Overview • Understanding Inheritance • Designing an Inheritance Tree • Avoiding Duplicate Code • Overriding Methods • IS-A and HAS-A • What is inherited from a superclass • What does inheritance really help with • Polymorphism • Rules for Overriding • Method Overloading

  3. Understanding Inheritance • A subclass inherits from a superclass –or- A subclass extends a superclass • Class methods can be overridden but not instance variables

  4. Understanding Inheritance • Superclass • Doctor • Subclasses • Family Doctor • Surgeon

  5. Designing an Inheritance Tree • If you have multiple objects that need to inherit from other objects you need and inheritance tree • Inheritance trees show which objects are sub classes to other objects • They show what methods are overridden at each level

  6. Overriding Methods • When you have a class tree, the lowest method or most specific method is called first. wolf.roam calls from class Canine wolf.eat calls from class Wolf, not class Animal llama.eat calls from class Animal, not class Wolf

  7. IS-A and HAS-A • Java lets us decide that anything can inherit from anything else • Some Superclass/Subclass relationship do not make sense • Use the IS-A and HAS-A test to decide • Using the lists below, decide which items should be superclasses and which subclasses or which should be neither • List1: Room, light switch, carpet, closet • List2: Automobile, Wheel, Garage, Truck

  8. What is inherited from a superclass • When a subclass inherits from a super class, it does not inherit private members • Only public members are inherited

  9. What does inheritance really help with • In summary the concept of inheritance: • Allows for reducing duplicate code • Allows for the definition of a common protocol for a group of objectsprotocol (def.): The established code of procedure or behavior in any group, organization, or situation. • Polymorphism

  10. Polymorphism • Polymorphism lets you link a super class object reference to a subclass object. Before Polymorphism

  11. More Polymorphism Animal[] animals = new Animal[3]; animals[0] = new Dog(); animals[1] = new Cat(); animals[2] = new Lion(); ..... for(int i = 0; i<animals.length; i++){ animals[i].eat(); animals[i].roam(); }

  12. More Polymorphism class Vet { public void giveShot(Animal a){ // Give animal a shot a.makeNoise(); } }

  13. Rules for Overriding • Overrides must have the same return types and arguments in the sub and super class to work. • Overrides need to be as public ormore in the subclass than in the superclass.

  14. Method Overloading • Overloading lets us change the a function based on its inputs • Powerful tool for handling different data types • System.out.println() is an overloaded method System.out.println("String"); //Take a string System.out.println(5); //Take an int System.out.println('5'); //Take a character

  15. Another example

  16. Things to remember • Inheritance is powerful but takes thought • Using overriding makes using multiple object easier • Using overloading gives more versatility to your methods

More Related