1 / 15

Inheritance and Polymorphism

Inheritance and Polymorphism. Object-oriented programming. Key aspects of object-oriented programming Reusability/Encapsulation Inheritance Polymorphism We have utilized Reusability and encapsulation throughout the semester. Inheritance.

berne
Télécharger la présentation

Inheritance and Polymorphism

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. Inheritance and Polymorphism

  2. Object-oriented programming • Key aspects of object-oriented programming • Reusability/Encapsulation • Inheritance • Polymorphism • We have utilized Reusability and encapsulation throughout the semester

  3. Inheritance • Inheritance allows you to create new classes that extend the functionality of existing classes • The new classes are called “subclasses” of the “parent” class • Just as children inherit features from their parents (skin color, eye color, etc) subclasses inherit properties and methods from their parent class.

  4. Subclasses • Can use the methods and properties inherited from the super (parent) class • Can redefine the methods and properties inherited from the super (parent) class • Can add new methods and properties to extend the super class

  5. Specifying parents • The keyword “extends” tells Java which class the new class should inherit from. • Any new code that is added is specific to the new class only. (Your mother doesn’t inherit your blue eyes!) • Inherited properties/methods may be handed down through multiple generations. • Your father inherits your grandfather’s strong chin, you inherit it from your father.

  6. Example • A company has three different types of employees: • Those paid a yearly salary • Those paid an hourly wage • Those paid by commission

  7. Three classes? Salaried employee Waged employee Commissioned employee First name Last name Employee ID Address Phone Salary Weekly pay First name Last name Employee ID Address Phone Wage Hours worked Weekly pay First name Last name Employee ID Address Phone Commission Weekly sales Weekly pay Constructor determineWeeklyPay Getters Setters Constructor determineWeeklyPay Getters Setters Constructor determineWeeklyPay Getters Setters

  8. When to create subclasses • When you need a class that shares several properties and methods with another class • Benefits • Super class is developed and tested once • Subclasses require less work and testing since much is already done

  9. Create a super class and subclasses

  10. Employee First name Last name Employee ID Address Phone Weekly pay Constructor Getters Setters Commissioned employee Salaried employee Waged employee Commission Weekly sales Wage Hours worked Salary determineWeeklyPay determineWeeklyPay determineWeeklyPay

  11. A real world Example: JButton Java.lang.Object Java.awt.Component Java.awt.Container Javax.swing.JCompoment Javax.swing.AbstractButton Javax.swing.JButton

  12. Polymorphism • Polymorphism is the capability of a method to do different things based on the object that it is acting upon. • Overloaded methods • methods with the same name signature but either a different number of parameters or different types in the parameter list. • Overridden methods • methods that are inherited but redefined within a subclass. They have the same signature and the subclass definition is used.

  13. Method overloading in our GrayscaleImage Class

  14. Method overriding in our GrayscaleImage Class public class GrayscaleImage extends JPanel { … /* * paint method * Purpose: override paint method of panel to permit display of image * Modifies: none * Input parameters: none (the graphics context, g, is supplied by java) * Returns: none */ public void paint(Graphics g) { if( bufferedImage != null) g.drawImage(bufferedImage,0,0, this); }//paint }//GrayscaleImage class

  15. Access modifiers • public • private • protected • Any derived class can access • Any method in the same package can access • package (default) • When you do not specify an access method • Any method in the same package can access • No one else, even if derived, if not in the same package • The preferred modifier

More Related