Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14.

Similar presentations


Presentation on theme: "Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14."— Presentation transcript:

1 Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 14

2 Note Set 14 Overview Inheritance & Polymorphism

3 Scenario You’re expanding your math practice program. It will: Create problems of various types Create problems of various difficulties Model the problems themselves as objects What do they have in common? How do each differ?

4 Inheritance Hierarchy Problem DivisionProblem MultiplicationProblem SubtractionProblem AdditionProblem Can treat a subclass object as if it were a superclass object Problem p = new AdditionProblem();

5 Polymorphism Problem … … AdditionProblem … … … … - Problem implements the getAnswer() method that returns the answer/solution -Because there is no operation associated, always returns 0. Each subclass overrides the getAnswer() - redefines or re-implements that method

6 Polymorphism Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); We can treat a subclass object as a superclass object.

7 Polymorphism Problem … … AdditionProblem … … … … AdditionProblem p = new Problem(); Can’t do this – Problem is NOT an AdditionProblem

8 Polymorphism Problem … … AdditionProblem … … … … Problem p = new Problem(); //call the getAnswer method in problem p.getAnswer(); //always returns 0

9 Polymorphism Problem … … AdditionProblem … … … … AdditionProblem ap = new AdditionProblem(); //Calls getAnswer method in AdditionProblem ap.getAnswer();//returns the correct answer

10 Polymorphism – The POWER!!! Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); p.getAnswer();//returns the correct answer // for an additionProblem - p is a Problem variable – but it references an AdditionProblem object - We can only do this because AdditionProblem extends Problem - Call getAnswer on p - At runtime – the correct version of getAnswer() will be called – the one that goes with AdditionProblem object

11 Polymorphism – The POWER!!! Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); p.getAnswer();//returns the correct answer // for an additionProblem Why is this so powerful? Easily Extensible – Can add other problems types without affecting current implementation.

12 Abstract Classes and Methods Sometimes it doesn’t make sense to instantiate objects of a particular type in an inheritance hierarchy. Consider class Problem Does it make sense to ever instantiate an object of type Problem? Why not? Class Problem could be made an abstract super class by making getAnswer() and getOperator() abstract methods

13 Abstract Superclass abstract classes cannot be instantiated used only as superclasses in inheritance hierarchies Purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design Class is declared abstract by using keyword abstract Abstract classes usually have 1 or more abstract methods No implementation is provided – left to subclasses to implement

14 Problem public abstract class Problem { private int op1; private int op2; //Constructors //Accessors and Mutators public abstract int getAnswer(); public abstract String getOperator(); public String toString() { return Integer.toString(getOp1()) + " " + this.getOperator() + " " + Integer.toString(getOp2()) + " = “ + Integer.toString(getAnswer()); } Notice: No implementation provided – will be specific to subclass Problem p = new Problem(); //COMPILE ERROR //Can’t instantiate abstract class

15 Abstract classes – some details A class must be declared abstract if it contains at least one abstract method An abstract method from a superclass must be overridden in a subclass unless the subclass is also declared abstract Abstract classes can contain concrete methods and instance variables. These are inherited as in normal inheritance. Is this legal? Problem [] p = new Problem [4];

16 Example 2 A company pays its employees on a weekly basis. There are four types of employees: 1.Salaried employees are paid a fixed weekly salary 2.Hourly employees are paid by the hour and receive 1.5 hourly rate for over 40 hours of work 3.Commission employees are paid a percentage of their sales Salaried and commission employees are eligible for a bonus based on the total profit generated for the company.


Download ppt "Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14."

Similar presentations


Ads by Google