Download presentation
Presentation is loading. Please wait.
Published byEugenia Sutton Modified over 9 years ago
1
C HAPTER 7 Better Living in Objectville
2
O VERVIEW 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
U NDERSTANDING I NHERITANCE A subclass inherits from a superclass –or- A subclass extends a superclass Class methods can be overridden but not instance variables
4
U NDERSTANDING I NHERITANCE Superclass Doctor Subclasses Family Doctor Surgeon
5
D ESIGNING AN I NHERITANCE T REE 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
O VERRIDING M ETHODS 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
W HAT 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
W HAT 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 objects protocol (def.): The established code of procedure or behavior in any group, organization, or situation. Polymorphism
10
P OLYMORPHISM Polymorphism lets you link a super class object reference to a subclass object. Before Polymorphism
11
M ORE P OLYMORPHISM 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
M ORE P OLYMORPHISM class Vet { public void giveShot(Animal a){ // Give animal a shot a.makeNoise(); }
13
R ULES FOR O VERRIDING Overrides must have the same return types and arguments in the sub and super class to work. Overrides need to be as public or more in the subclass than in the superclass.
14
M ETHOD O VERLOADING 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
A NOTHER EXAMPLE
16
T HINGS TO REMEMBER Inheritance is powerful but takes thought Using overriding makes using multiple object easier Using overloading gives more versatility to your methods
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.