Inheritance and Polymorphism

carenextΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

121 εμφανίσεις


Key aspects of object
-
oriented
programming


Reusability/Encapsulation


Inheritance


Polymorphism



We have utilized Reusability and
encapsulation throughout the semester





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.


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


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.


A company has three different types of
employees:

1.
Those paid a yearly salary

2.
Those paid an hourly wage

3.
Those paid by commission

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

Salaried employee

Salary

determineWeeklyPay

Waged employee

Wage

Hours worked

determineWeeklyPay

Commissioned

employee

determineWeeklyPay

Commission

Weekly sales

Java.lang.Object

Java.awt.Component

Java.awt.Container

Javax.swing.JCompoment

Javax.swing.AbstractButton

Javax.swing.JButton

A real world Example:
JButton


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 overloading 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

Method overriding in our
GrayscaleImage

Class


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