Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming

2 Reducing User Complexity Inheritance makes development easier May make things more complex for user –Need to construct, store, manipulate many different types of objects Employee HourlySalaried Shareholder

3 Reducing User Complexity Goal of polymorphism: Allow user to treat all objects as instances of the superclass Employee HourlySalaried Shareholder User just needs to know how to use superclass Subclasses can be treated like part of internal representation

4 Polymorphism Can assign subclass object to superclass variable Clock c = new SecondClock (12,34,56); Can make changes dynamically Clock c; c = new Clock(14,34); c = new SecondClock(12,34,56);

5 Polymorphisn How is this possible? Subclass object has all methods of superclass –Inherited –Overridden Clock c = new SecondClock(12,34,56); c.setHour(8); c.nextMinute(); s = c.toString(); … All of these methods also defined for SecondClock objects!

6 Polymorphism Note: can only use superclass methods Clock c = new SecondClock(12,34,56); c.setHour(9); c.setMinute(21); c.setSecond(30); Methods used in polymorphism must be defined in the superclass Legal, since setHour and setMinute defined for both Clock and SecondClock Illegal, since setSecond only defined for SecondClock

7 Run-time Evaluation JVM chooses version of overridden method based on actual object type Example: overridden toString() method Clock c; c = new Clock(14,34); System.out.println(c.toString()); c = new SecondClock(12,34,56); System.out.println(c.toString()); Uses Clock ’s toString() “ 14:34 ” Uses SecondClock ’s toString() “ 12:34:56 ”

8 Run-time Evaluation Note that this decision must be made at run time based on current type of object Clock c; if (Math.random() > 0.5) c = new Clock(14,34); else c = new SecondClock(12,34,56); System.out.println(c.toString()); Tradeoff of representational power vs. run speed –C++ requires such methods be explicitly declared “virtual” –By default, makes decision about object type at compile time Will not know which version to use until run this line of code

9 Uses of Polymorphism Storing different types in single data structure –Define array of superclass type –Store subclass objects in it Employee workers = new Employee[100]; workers[0] = new Hourly(“Fred”, 10, 40); workers[1] = new Salaried(“Barney”, 26000); workers[2] = new Shareholder(“Slade”, 52000, 1000); …

10 Uses of Polymorphism Defining single method that takes multiple types of parameter –Use superclass type in method definition –Pass subclass objects when called JPanel panel = new JPanel(); p.add(infoLabel); // add JLabel p.add(nameField); // add JTextField p.add(startButton); // add JButton …

11 Uses of Polymorphism add method in JPanel takes JComponent as parameter Superclass of all visual components –Defines x, y, width, and height of component –All the JPanel needs to display on surface public class JPanel { … public void add(JComponent c) { … … JPanel p = new JPanel(); p.add(infoLabel); Assigning subclass object to superclass variable


Download ppt "Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google