Download presentation
Presentation is loading. Please wait.
Published byLorin Price Modified over 8 years ago
1
Inheritance & Polymorphism Android Club 2015
2
Agenda Inheritance Polymorphism
3
WHY to use inheritance? CODE REUSE
4
Inheritance
5
Inheritance: example public class Bike { int wheel; public Bike() { this.wheel = 2; } public int getWheel() { return wheel; } public void setWheel(int wheel) { this.wheel = wheel; } }
6
Inheritance: example public class MountainBike extends Bike{ public MountainBike() { super(); }
7
Inheritance: example public class KidBike extends Bike{ public KidBike() { setWheel(3); }
8
Inheritance: practice Create super class: Transport Create variables: int wheel + getter&setter Default wheels number should be 4 Create sub-class: Car Create Car constructor: inside call Transport constructor Create sub-class: Boat Set wheel 0
9
Polymorphism Two objects respond to the same message with different behaviors; the sender doesn't have to care. Steven A. Lowe Peter Mortensen
10
Polymorphism There are: 2 objects Message: makeNoise Cat: meow Dog: wow Sender does not care
11
Polymorphism: Animal public class Animal { public String getNoise() { return null; }
12
Polymorphism: Cat public class Cat extends Animal{ public String getNoise() { return "Meow"; }
13
Polymorphism: Dog public class Dog extends Animal{ public String getNoise() { return "Wow"; }
14
Polymorphism: main Animal animal1 = new Cat(); System.out.println(animal1.getNoise()); Animal animal2 = new Dog(); System.out.println(animal2.getNoise());
15
Polymorphism: practice Create super-class: Zombie Create method: eat Return: void
16
Polymorphism: practice Create sub class: GardenZombie Create method: eat “Eats 5 pieces”
17
Polymorphism: practice Create sub class: Gargantua Create method: eat Print “Eats 10 pieces”
18
Polymorphism: practice Declare Zombie zombieOne Initialize it as GardenZombie Make zombieOne eat Declare Zombie: zombieTwo Initialize it as Gargantua Make zombieTwo eat
19
Questions? Any questions?
20
Review Chapter 10: Working with Inheritance and Polymorphism Interface
21
Thank you! Thank you very much for your attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.