60 likes | 201 Views
Week 17. Compare & Contrast Simpson's Puzzle solutions 9 Marks [ ]. This week…. Take a look at a solution to week 15’s Simpson's puzzle. What is good & what is bad? How can we improve on this? Given that Java in a nice OO language Polymorphism
E N D
Week 17 Compare & Contrast Simpson's Puzzle solutions 9 Marks [ ]
This week…. • Take a look at a solution to week 15’s Simpson's puzzle. • What is good & what is bad? • How can we improve on this? • Given that Java in a nice OO language • Polymorphism • What would a more elegant solution look like?
Remember references? String user_name = new String(“THE”); Pointer to point to string object (called a Reference in Java) Allocate Storage in memory for a String object Connect user_name
Polymorphism! • A very handy OO concept • Any instance of a class which inherits from (extends) another can also be treated as an instance of that class. • Casting is the process of converting an object from one type to another • e.g. converting int to float • Can also be used with object references… • e.g. ((JButton)e.getSource()).setIcon(…)
Casting Object References • In java, any instance is implicitly and transparently casted as necessary. • Provided that the transformation goes upwards the inheritance hierarchy
Inheritance Hierarchies (Casting) Automatic