160 likes | 339 Views
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.
E N D
Object-oriented programming • Key aspects of object-oriented programming • Reusability/Encapsulation • Inheritance • Polymorphism • We have utilized Reusability and encapsulation throughout the semester
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.
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
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.
Example • A company has three different types of employees: • Those paid a yearly salary • Those paid an hourly wage • Those paid by commission
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
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
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
A real world Example: JButton Java.lang.Object Java.awt.Component Java.awt.Container Javax.swing.JCompoment Javax.swing.AbstractButton Javax.swing.JButton
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.
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
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