1 / 16

CSE 1341 - Honors Principles of Computer Science I

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

werner
Download Presentation

CSE 1341 - Honors Principles of Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu 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 AdditionProblem SubtractionProblem MultiplicationProblem DivisionProblem Can treat a subclass object as if it were a superclass object Problem p = new AdditionProblem();

  5. 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

  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 … … … AdditionProblemap = 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 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

  15. 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.

  16. 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.

More Related