3/26/15

OOP

• Object-oriented programming is another paradigm that makes objects its central Inheritance and Polymorphism players, not funcons. • Objects are pieces of data and the associated behavior. (with Pokemon) • Classes define an object, and can inherit methods and instance variables from each other.

2

Inheritance Inheritance

Occasionally, we find that many abstract data We would like to have different kinds of Pokémon, types are related. which differ (among other things) in the amount of points lost by its opponent during an aack.

For example, there are many different kinds of The only method that changes is attack. All the people, but all of them have similar methods of other methods remain the same. Can we avoid eang and sleeping. duplicang code for each of the different kinds?

3 4

Inheritance Inheritance

Key OOP Idea: Classes can inherit methods and Key OOP Idea: Classes can inherit methods and instance variables from other classes instance variables from other classes public class WaterPokemon extends Pokemon public class WaterPokemon extends Pokemon { { … … void attack(Pokemon other) void attack(Pokemon other) The Pokemon class is the superclass of the { { class. other.decrease_hp(75); other.decrease_hp(75); WaterPokemon } } The WaterPokemon } } class is the subclass of the Pokemon class.

5 6

1 3/26/15

Inheritance Inheritance

Key OOP Idea: Classes can inherit methods and instance variables from other classes WaterPokemon ashs_squirtle = new WaterPokemon(“”,”Ash”, 314); Pokemon mistys_togepi = new Pokemon(“”, public class WaterPokemon extends Pokemon { “Misty”, 245); … mistys_togepi.attack(ashs_squirtle); void attack(Pokemon other) System.out.println(ashs_squirtle.getHitPts()); { The attack method 264 Pokemon class is other.decrease_hp(75); from the ashs_squirtle.attack(mistys_togepi); overridden by the } attack method from the System.out.println(mistys_togepi.getHitPts()); } WaterPokemon class. 170

7 8

Inheritance Inheritance

WaterPokemon ashs_squirtle = new WaterPokemon ashs_squirtle = new WaterPokemon(“Squirtle”,”Ash”, 314); WaterPokemon(“Squirtle”,”Ash”, 314); Pokemon mistys_togepi = new Pokemon(“Togepi”, Pokemon mistys_togepi = new Pokemon(“Togepi”, “Misty”, 245); “Misty”, 245); mistys_togepi.attack(ashs_squirtle); mistys_togepi.attack(ashs_squirtle);

System.out.println(ashs_squirtle.getHitPtsmistys_togepi()); System.out.println(ashs_squirtle.getHitPts()); 264 uses the attack 264 method from the ashs_squirtle.attack(mistys_togepi); Pokemon class. ashs_squirtle.attack(mistys_togepi); System.out.println(mistys_togepi.getHitPts()); System.out.println(mistys_togepi.getHitPtsashs_squirtle()); 170 170 uses the attack method from the WaterPokemon class.

9 10

Inheritance Review: Inheritance

WaterPokemon ashs_squirtle = new If the class of an object has the method or aribute of WaterPokemon(“Squirtle”,”Ash”, 314); interest, that parcular method or aribute is used. Pokemon mistys_togepi = new Pokemon(“Togepi”, “Misty”, 245); mistys_togepi.attack(ashs_squirtle); Otherwise, the method or aribute of its parent is used. System.out.println(ashs_squirtle.getHitPts()); 264 Inheritance can be many levels deep. ashs_squirtle.attack(mistys_togepi); The WaterPokemon System.out.println(mistys_togepi.getHitPtsclass does not have a ()); If the parent class does not have the method or aribute, getHitPts method, 170 so it uses the method we check the parent of the parent class, and so on. from its superclass.

11 12

2 3/26/15

Review: Inheritance Review: Inheritance

public class ElectricPokemon extends Pokemon We can also both override a parent’s method or { aribute and use the original parent’s method. … We use the original attack method void attack(Pokemon other) from the parent. { Say that we want to modify the aacks of super.attack(other); Electric Pokémon: when they aack another increase_hp(10); Pokémon, the other Pokémon loses the original } } 50 HP, but the Electric Pokémon gets an increase of 10 HP.

13 14

Polymorphism Polymorphism

Write the method attackAll for the Pokemon class that takes an array of public class Pokemon Pokemon objects as its argument. When called on a Pokemon object, that Pokémon will aack each of the Pokémon in the provided array. (Ignore the { output printed by the attack method.) … void attackAll(Pokemon[] others) >>> ashs_pikachu = ElectricPokemon(‘’, ‘Ash’, 300); >>> ashs_squirtle = WaterPokemon(‘Squirtle’, ‘Ash’, 314); { >>> mistys_togepi = Pokemon(‘Togepi’, ‘Misty’, 245); for(Pokemon other : others) >>> mistys_togepi.attackAll([ashs_pikachu, ashs_squirtle]); attack(other); >>> ashs_pikachu.getHitPts(); } 250 >>> ashs_squirtle.getHitPts(); } 264

15 16

Polymorphism Polymorphism

for(Pokemon other : others) for(Pokemon other : others) attack(other); attack(other);

other can be an object of many different data attack can work on objects of many types: ElectricPokemon, WaterPokemon, different data types, without having to and Pokemon, for example. consider each data type separately.

attack is polymorphic. poly = Many morph = Forms

17 18

3 3/26/15

Polymorphism Polymorphism

public class Pokemon Write the method attackedBy for the Pokemon class that takes an array of { Pokemon objects as its argument. When called on a Pokemon object, that … Pokémon will be aacked by each of the Pokémon in the provided array. void attackedBy(Pokemon[] others) >>> ashs_pikachu = ElectricPokemon(‘Pikachu’, ‘Ash’, 300); { >>> ashs_squirtle = WaterPokemon(‘Squirtle’, ‘Ash’, 314); for(Pokemon other : others) >>> mistys_togepi = Pokemon(‘Togepi’, ‘Misty’, 245); >>> ashs_squirtle.attackedBy([ashs_pikachu, mistys_togepi]); other.attack(this); >>> ashs_squirtle.getHitPts(); } 204 }

19 20

Polymorphism Polymorphism

for(Pokemon other : others) Key OOP idea: The same method can work on other.attack(this); data of different types.

other can be an object of many different data We have seen a polymorphic funcon before: types: ElectricPokemon, WaterPokemon, and Pokemon, for example. 3 + 4 The + operator, or the à7 add funcon, is polymorphic. It can It can also be an object of any other class that has ‘hello’ + ‘ world’ work with both an attack method. à‘hello world’ numbers and strings.

21 22

Everything is an Object Everything is an Object

The Object class provides extra methods that Everything (except primives) in Java enable polymorphic methods like equals or toString to work on many different forms of is an object. data.

In parcular, every class is a subclass of a built-in Object class.

This is not the term ‘object’, but . a class whose name is Object 23 24

4 3/26/15

Everything is an Object Everything is an Object

Implement the method equals for the Implement the method equals for the Pokemon class that will allow us to check if one Pokémon is “equal” to Pokemon class that will allow us to check if one another, which means that it has the same name as Pokémon is “equal” to another, which means another. that it has the same name as another. public class Pokemon { ashs_pikachu = ElectricPokemon(‘Pikachu’, ‘Ash’, … 300); boolean equals(Pokemon p) brocks_pikachu = ElectricPokemon(‘Pikachu’, ‘’, { 300); return ______; ashs_pikachu.equals(brocks_pikachu); } àtrue }

25 26

Everything is an Object Everything is an Object

Implement the method equals for the Pokemon class The Object class provides a default equals that will allow us to check if one Pokémon is “equal” to another, which means that it has the same name as method, but the equals method we defined another. for the Pokemon class overrides the method provided by the object class, as we would public class Pokemon expect from inheritance. { … boolean equals(Pokemon p) { Boom line: return this.name.equals(p.name); Inheritance and polymorphism in OOP allow us } } to override standard methods!

27 28

Everything is an Object Conclusion

Side-note: • Inheritance and polymorphism are two key Every class inherits from the object class, ideas in OOP. including the Pokemon class. – Inheritance allows us to establishes relaonships (and reuse code) between two similar data types. – Polymorphism allows funcons to work on many public class Pokemon types of data/objects. is shorthand for • (Almost) Everything is an object. public class Pokemon extends Object • OOP allows us to override standard methods.

29 30

5