160 likes | 259 Views
CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 14. Note Set 14 Overview. Inheritance & Polymorphism. Scenario. You’re expanding your math practice program. It will: Create problems of various types
E N D
CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu Note Set 14
Note Set 14 Overview Inheritance & Polymorphism
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?
Inheritance Hierarchy Problem AdditionProblem SubtractionProblem MultiplicationProblem DivisionProblem Can treat a subclass object as if it were a superclass object Problem p = new AdditionProblem();
Polymorphism Problem • - Problem implements the • getAnswer() • method that returns the answer/solution • Because there is no operation associated, always returns 0. AdditionProblem … … … Each subclass overrides the getAnswer() - redefines or re-implements that method
Polymorphism Problem AdditionProblem … … … Problem p = new AdditionProblem(); We can treat a subclass object as a superclass object.
Polymorphism Problem AdditionProblem … … … AdditionProblem p = new Problem(); Can’t do this – Problem is NOT an AdditionProblem
Polymorphism Problem AdditionProblem … … … Problem p = new Problem(); //call the getAnswer method in problem p.getAnswer(); //always returns 0
Polymorphism Problem AdditionProblem … … … AdditionProblemap = new AdditionProblem(); //Calls getAnswer method in AdditionProblem ap.getAnswer();//returns the correct answer
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
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.
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
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
Problem public abstract class Problem {private int op1; private int op2; //Constructors //Accessors and Mutators public abstractintgetAnswer(); 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
Abstract classes – some details Is this legal? Problem [] p = new Problem [4]; 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.
Example 2 A company pays its employees on a weekly basis. There are four types of employees: Salaried employees are paid a fixed weekly salary Hourly employees are paid by the hour and receive 1.5 hourly rate for over 40 hours of work 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.