JAVA

JAVA
Object Oriented Programming
Objectives
Be able to implement inheritance using
extends
, 
super
, and 
abstract
Be able to describe differences between a
concrete and abstract class.
Be able to recognize polymorphic behavior
 
3
 
You can get stuck at the object-based level because
you can quickly get there and you get a lot of benefit
without much mental effort. It’s also easy to feel like
you’re creating data types – you make classes and
objects, you send messages to those objects, and
everything is nice and neat.
 
But don’t be fooled. If you stop here, you’re missing
out on the greatest part of the language, which is the
jump to true object-oriented programming. You can do
this only with virtual [abstract] functions.
    
 
 
- 
Bruce Eckel,
 
Thinking in C++, 
Example: Analysis
We want to make a payroll program that
lists each employee, some information
about them, and their pay.
4
...
Employee e = new Employee(0, ”Bashful", 
  
   
"customer representative”,
     
10.0, 40.0);
System.out.println(e);
System.out.println(e.computePay());
...
OUTPUT:
0 Bashful
 
Role: customer representative
 
Payrate: 10.0
 
Hours Worked: 40.0
 
Pay: $400.0
5
Adding More Employee Types
 
Our initial iteration’s design assumes that all employees
are paid in exactly the same way
If we want a different kind of employee, we need to create
an entirely new type for it
Bashful was an hourly employee, what about a salaried
employee?
What would be the same?
ID, Name, Role
What would be different?
No mention of hours, salary instead of payRate
6
Modeling Objects
Object-Oriented programming models objects and the
relationships between them.
Examples:
figures, squares, rectangles, polygons, doodles;
people, students, teachers, staff members, you;
animals, birds, penguins, wings,         ;
naval vessels, submarines, carriers, fighter jets.
image from http://www.linux.org/info/penguin.html
7
Modeling and Implementing Relationships
 
We have seen two inter-object relationships:
i
s
 
a
 
 
i
n
s
t
a
n
t
i
a
t
i
o
n
implemented using constructor methods (for
reference objects);
h
a
s
 
a
 
 
a
g
g
r
e
g
a
t
i
o
n
implemented using instance data references;
specifies container-contained relationships
between classes.
The container references the contained.
8
Modeling and Implementing Relationships
Inheritance adds another kind of relationship:
implemented using extends clauses;
9
Class Inheritance
Inheritance
 specifies one-directional,
parent-child relationships.
i
s
 
a
 
k
i
n
d
 
o
f
 
 
i
m
p
l
e
m
e
n
t
e
d
 
u
s
i
n
g
e
x
t
e
n
d
s
 
c
l
a
u
s
e
The child inherits the parent’s:
data
methods
Each child can 
polymorphically
“specialize” itself by 
overriding
 or adding
data or methods.
10
Systema Naturae
, 1758
7 levels:
Kingdom
Phylum
Class
Order
Family
Genus
Species
Carl Linneaus 
(1707-1778)
Taxonomy
images from www.linnean.org  & www.kheper.auz.com
11
Example: Java’s Classes
All Java classes fit into one hierarchy.
All classes inherit from the root class:
  
java.lang.Object
You can find the full Java hierarchy here:
http://java.sun.com/javase/6/docs/api/overview-tree.html
12
Example: Design of Employees
13
Implementing Inheritance
A child class specifies inheritance from a parent class
using the 
extends
 clause.
J
a
v
a
 
p
r
o
v
i
d
e
s
 
t
h
r
e
e
 
m
o
d
i
f
i
e
r
s
 
s
p
e
c
i
f
y
i
n
g
 
a
c
c
e
s
s
 
f
o
r
 
c
l
a
s
s
v
a
r
i
a
b
l
e
s
 
a
n
d
 
m
e
t
h
o
d
s
:
private
protected
public
It can be safer to declare attributes as private and provide
protected accessor and mutator methods.
14
Super-Class Constructors
super
 is a reference to the parent.
A child can invoke its parent’s constructor.
super(
parentConstructorArguments
)
A
 
c
a
l
l
 
t
o
 
t
h
e
 
p
a
r
e
n
t
s
 
c
o
n
s
t
r
u
c
t
o
r
:
must be the first statement in the constructor;
is added automatically if you don’t add it.
A child can invoke 
an overridden method:
 
super.
parentMethod
(
arguments
)
p
u
b
l
i
c
 
c
l
a
s
s
 
E
m
p
l
o
y
e
e
 
{
p
r
i
v
a
t
e
 
i
n
t
 
m
y
I
d
;
p
r
i
v
a
t
e
 
S
t
r
i
n
g
 
m
y
N
a
m
e
,
 
m
y
R
o
l
e
;
p
u
b
l
i
c
 
E
m
p
l
o
y
e
e
(
)
 
{
  
myId = 0;
  
myName = myRole = "";
 
}
p
u
b
l
i
c
 
E
m
p
l
o
y
e
e
(
i
n
t
 
i
d
,
 
S
t
r
i
n
g
 
n
a
m
e
,
 
S
t
r
i
n
g
 
r
o
l
e
)
 
{
  
myId = id;
  
myName = name;
  
myRole = role;
 
}
 
/
/
a
c
c
e
s
s
o
r
s
 
a
n
d
 
m
u
t
a
t
o
r
s
 
o
m
i
t
t
e
d
 
f
o
r
 
s
p
a
c
e
 
@Override
p
u
b
l
i
c
 
S
t
r
i
n
g
 
t
o
S
t
r
i
n
g
(
)
 
{
r
e
t
u
r
n
 
m
y
I
d
 
+
 
"
 
"
 
+
 
m
y
N
a
m
e
 
+
 
"
\
n
\
t
R
o
l
e
:
 
"
 
+
 
m
y
R
o
l
e
;
 
}
}
p
u
b
l
i
c
 
c
l
a
s
s
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
 
e
x
t
e
n
d
s
 
E
m
p
l
o
y
e
e
 
{
p
r
i
v
a
t
e
 
s
t
a
t
i
c
 
f
i
n
a
l
 
d
o
u
b
l
e
 
W
O
R
K
_
W
E
E
K
 
=
 
4
0
.
0
,
 
O
V
E
R
T
I
M
E
_
R
A
T
E
 
=
 
1
.
5
;
p
r
i
v
a
t
e
 
d
o
u
b
l
e
 
m
y
P
a
y
r
a
t
e
,
 
m
y
H
o
u
r
s
;
p
u
b
l
i
c
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
)
 
{
  
myPayrate = myHours = 0.0;
 
}
p
u
b
l
i
c
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
i
n
t
 
i
d
,
 
S
t
r
i
n
g
 
n
a
m
e
,
 
S
t
r
i
n
g
 
r
o
l
e
,
 
d
o
u
b
l
e
 
p
a
y
r
a
t
e
,
d
o
u
b
l
e
 
h
o
u
r
s
)
 
{
s
u
p
e
r
(
i
d
,
 
n
a
m
e
,
 
r
o
l
e
)
;
  
myPayrate = payrate;
  
myHours = hours;
 
}
 
 
//accessor and mutators omitted for space,  compute pay on comparison slide
 
 
 
@Override
p
u
b
l
i
c
 
S
t
r
i
n
g
 
t
o
S
t
r
i
n
g
(
)
 
{
r
e
t
u
r
n
 
s
u
p
e
r
.
t
o
S
t
r
i
n
g
(
)
 
+
 
"
\
n
\
t
P
a
y
r
a
t
e
:
 
"
 
+
 
m
y
P
a
y
r
a
t
e
 
+
 
 
 
 
 
"
\
n
\
t
H
o
u
r
s
 
w
o
r
k
e
d
:
 
"
 
+
 
m
y
H
o
u
r
s
;
 
}
}
17
Example: Design of Employees
p
u
b
l
i
c
 
c
l
a
s
s
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
 
e
x
t
e
n
d
s
 
E
m
p
l
o
y
e
e
 
{
 
p
r
i
v
a
t
e
 
d
o
u
b
l
e
 
m
y
S
a
l
a
r
y
;
 
p
u
b
l
i
c
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
)
 
{
  
mySalary = 0.0;
 
}
 
p
u
b
l
i
c
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
i
n
t
 
i
d
,
 
S
t
r
i
n
g
 
n
a
m
e
,
 
S
t
r
i
n
g
 
r
o
l
e
,
 
d
o
u
b
l
e
 
s
a
l
a
r
y
)
 
{
s
u
p
e
r
(
i
d
,
 
n
a
m
e
,
 
r
o
l
e
)
;
  
mySalary = salary;
 
}
 
 
//accessors, mutators omitted for space, computePay() on following
 
 
@Override
p
u
b
l
i
c
 
S
t
r
i
n
g
 
t
o
S
t
r
i
n
g
(
)
 
{
r
e
t
u
r
n
 
s
u
p
e
r
.
t
o
S
t
r
i
n
g
(
)
 
+
 
"
\
n
\
t
A
n
n
u
a
l
 
S
a
l
a
r
y
:
 
"
 
+
 
m
y
S
a
l
a
r
y
;
 
}
}
 
//Compute pay for hourly employee
 
@Override
p
u
b
l
i
c
 
d
o
u
b
l
e
 
c
o
m
p
u
t
e
P
a
y
(
)
 
{
i
f
 
(
m
y
H
o
u
r
s
 
<
=
 
W
O
R
K
_
W
E
E
K
)
 
{
r
e
t
u
r
n
 
m
y
P
a
y
r
a
t
e
 
*
 
m
y
H
o
u
r
s
;
}
 
e
l
s
e
 
{
r
e
t
u
r
n
 
(
m
y
P
a
y
r
a
t
e
 
*
 
W
O
R
K
_
W
E
E
K
)
 
+
 
 
 
 
 
 
 
(
(
m
y
P
a
y
r
a
t
e
 
*
 
O
V
E
R
T
I
M
E
_
R
A
T
E
)
 
*
 
(
m
y
H
o
u
r
s
 
-
 
W
O
R
K
_
W
E
E
K
)
)
;
  
}
 
}
 
//Compute pay for salaried employee
 
@Override
p
u
b
l
i
c
 
d
o
u
b
l
e
 
c
o
m
p
u
t
e
P
a
y
(
)
 
{
r
e
t
u
r
n
 
m
y
S
a
l
a
r
y
;
 
}
Comparison of computePay()
H
o
u
r
l
y
S
a
l
a
r
i
e
d
p
u
b
l
i
c
 
c
l
a
s
s
 
P
a
y
r
o
l
l
C
o
n
s
o
l
e
 
{
p
u
b
l
i
c
 
s
t
a
t
i
c
 
v
o
i
d
 
m
a
i
n
(
S
t
r
i
n
g
[
]
 
a
r
g
s
)
 
{
  
// 1. Employee only
  
 Employee e = new Employee(0, ”Bashful", "customer representative");
  
 System.out.println(e);
  
  
// 2. HourlyEmployee
H
o
u
r
l
y
E
m
p
l
o
y
e
e
 
h
e
 
=
 
n
e
w
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
)
;
  
he.setPayrate(10.0);
  
he.setHours(50.0);
  
System.
out.println(he.computePay());
  
he.setId(1);
  
he.setName("Sneezy");
  
he.setRole("intern");
  
System.
out.println(he);
  
  
// 3. SalariedEmployee
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
 
s
e
 
=
 
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
)
;
  
se.setId(2);
  
se.setName("Doc");
  
se.setRole("physician");
  
se.setSalary(100000.0);
  
System.out.println(
se);
  
  
System.out.println(se.computePay());
 
}}
Fruit Example
Fruit
Apple
Gala
Orange
Fuji
VALID OR INVALID??
Fruit fruit = new Gala();
Orange orange = new Orange();
Orange p = new Apple();
Gala p = new Apple();
Apple p = new Fuji();
Examples
p
u
b
l
i
c
 
c
l
a
s
s
 
T
e
s
t
 
{
p
u
b
l
i
c
 
s
t
a
t
i
c
 
v
o
i
d
 
m
a
i
n
(
S
t
r
i
n
g
[
]
 
a
r
g
s
)
 
 
{
B
 
b
 
=
 
n
e
w
 
B
(
)
;
       }
}
c
l
a
s
s
 
A
 
{
p
u
b
l
i
c
 
A
(
)
{
  
System.
out.println("A's constructor invoked");
 
}
}
c
l
a
s
s
 
B
 
e
x
t
e
n
d
s
 
A
{
}
Examples
p
u
b
l
i
c
 
c
l
a
s
s
 
T
e
s
t
 
{
p
u
b
l
i
c
 
s
t
a
t
i
c
 
v
o
i
d
 
m
a
i
n
(
S
t
r
i
n
g
[
]
 
a
r
g
s
)
 
 
{
B
 
b
 
=
 
n
e
w
 
B
(
)
;
       }
}
c
l
a
s
s
 
A
 
{
p
u
b
l
i
c
 
A
(
i
n
t
 
x
)
{
  
System.out.println("A's constructor invoked");
 
}
}
c
l
a
s
s
 
B
 
e
x
t
e
n
d
s
 
A
{
p
u
b
l
i
c
 
B
 
(
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
B
s
 
c
o
n
s
t
r
u
c
t
o
r
 
i
n
v
o
k
e
d
)
;
}
}
Examples
class Test {
 
public static void main(String[] args)  {
  
A a = new B();
  
System.out.println(a.doThis());
       }
}
class A {
 
public A(){
  
System.out.println("A's constructor invoked");
 
}
 
public String doThis(){
  
return “A’s doThis()”;
 
}
}
class B extends A{
 
public B (){
  
System.out.println(“B’s constructor invoked”);
 
}
 
public String doThis(){
  
return “B’s doThis()”;
 
}
}
Back to our goal
We want to be able to print out payroll information:
A
r
r
a
y
L
i
s
t
<
E
m
p
l
o
y
e
e
>
 
e
m
p
l
o
y
e
e
s
 
=
 
n
e
w
 
A
r
r
a
y
L
i
s
t
<
E
m
p
l
o
y
e
e
>
(
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
1
,
 
"
S
n
e
e
z
y
"
,
 
"
i
n
t
e
r
n
"
,
 
1
0
.
0
,
 
4
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
2
,
 
"
D
o
c
"
,
 
"
p
h
y
s
i
c
i
a
n
"
,
 
1
0
0
0
0
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
3
,
 
S
n
o
w
 
W
h
i
t
e
"
,
 
C
E
O
"
,
 
1
7
5
0
0
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
4
,
 
"
D
o
p
e
y
"
,
 
"
t
r
a
i
n
e
r
"
,
 
6
5
0
0
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
5
,
 
"
S
l
e
e
p
y
"
,
 
"
t
r
a
i
n
e
e
"
,
 
1
5
.
0
0
,
 
4
0
.
0
0
)
)
;
f
o
r
 
(
i
n
t
 
i
 
=
 
0
;
 
i
 
<
 
e
m
p
l
o
y
e
e
s
.
s
i
z
e
(
)
;
 
i
+
+
)
 
{
 
System.
out.println(employees.get(i));
 
System.
out.println("\tPay: $" + employees.get(i).computePay());
}
 
Problem: There is no 
computePay()
 for a generic Employee!
26
Abstract Classes
Classes can be abstract or concrete.
    
visibility
 abstract class 
className
 {
      
classDefinition
    }
Like concrete classes, abstract classes:
Can have sub-classes;
Can implement data and methods.
Unlike concrete classes, abstract classes:
Cannot be instantiated.
27
Abstract Methods
As with classes, methods can also be declared 
as
abstract
 or 
concrete
.
Abstract classes do not provide definitions for their
abstract methods.
Classes that contain abstract methods must be declared
as abstract.
visibility
 abstract 
type
 
methodName
(
params
);
28
Implementing Inheritance
A child class specifies inheritance from a parent class
using the 
extends
 clause.
Concrete sub-classes must define the abstract methods
that they inherit.
abstract class Parent {
  public abstract void aMethod();
}
class Child1 extends Parent {
  public void aMethod() {
    // 
define method here...
  }
}
Abstract Class
29
29
public 
abstract
 class Employee {
  
 
//private instance variables
  
 
public Employee(int id, String name, String role) {
  
myId = id;
  
myName = name;
  
myRole = role;
 
}
  
  
 
public abstract void computePay();
 
@Override
 
public String toString() {
  
return myId + " " + myName + "\n\tRole: " + myRole;
 
}
}
30
Overriding Methods
When an object to asked to execute a method, Java
searches up the inheritance hierarchy for a matching
method definition.
Thus, a sub-class that defines its own version of a method
overrides
 any definitions of the methods that it inherits.
A concrete sub-class must implement the abstract
methods it inherits using a method with an identical
signature
.
31
p
u
b
l
i
c
 
c
l
a
s
s
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
 
e
x
t
e
n
d
s
 
E
m
p
l
o
y
e
e
 
{
p
r
i
v
a
t
e
 
s
t
a
t
i
c
 
f
i
n
a
l
 
d
o
u
b
l
e
 
W
O
R
K
_
W
E
E
K
 
=
 
4
0
.
0
,
 
O
V
E
R
T
I
M
E
_
R
A
T
E
 
=
 
1
.
5
;
p
r
i
v
a
t
e
 
d
o
u
b
l
e
 
m
y
P
a
y
r
a
t
e
,
 
m
y
H
o
u
r
s
;
p
u
b
l
i
c
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
i
n
t
 
i
d
,
 
S
t
r
i
n
g
 
n
a
m
e
,
 
S
t
r
i
n
g
 
r
o
l
e
,
 
d
o
u
b
l
e
 
p
a
y
r
a
t
e
,
 
d
o
u
b
l
e
 
h
o
u
r
s
)
 
{
s
u
p
e
r
(
i
d
,
 
n
a
m
e
,
 
r
o
l
e
)
;
  
myPayrate = payrate;
  
myHours = hours;
 
}
 
@Override
p
u
b
l
i
c
 
d
o
u
b
l
e
 
c
o
m
p
u
t
e
P
a
y
(
)
 
{
i
f
 
(
m
y
H
o
u
r
s
 
<
=
 
W
O
R
K
_
W
E
E
K
)
 
{
r
e
t
u
r
n
 
m
y
P
a
y
r
a
t
e
 
*
 
m
y
H
o
u
r
s
;
}
 
e
l
s
e
 
{
r
e
t
u
r
n
 
(
m
y
P
a
y
r
a
t
e
 
*
 
W
O
R
K
_
W
E
E
K
)
 
+
 
(
(
m
y
P
a
y
r
a
t
e
 
*
 
O
V
E
R
T
I
M
E
_
R
A
T
E
)
 
*
 
(
m
y
H
o
u
r
s
 
-
 
W
O
R
K
_
W
E
E
K
)
)
;
  
}
 
}
 
@Override
p
u
b
l
i
c
 
S
t
r
i
n
g
 
t
o
S
t
r
i
n
g
(
)
 
{
r
e
t
u
r
n
 
s
u
p
e
r
.
t
o
S
t
r
i
n
g
(
)
 
+
 
"
\
n
\
t
P
a
y
r
a
t
e
:
 
"
 
+
 
m
y
P
a
y
r
a
t
e
 
+
 
"
\
n
\
t
H
o
u
r
s
 
w
o
r
k
e
d
:
 
"
 
+
 
m
y
H
o
u
r
s
;
 
}}
32
Polymorphism
Polymorphism
 allows different concrete
sub-classes to provide potentially different
definitions for a given method inherited
from their shared parent.
Java chooses the appropriate method
definition based upon which child the
object instantiates at either:
Compile time (
static
 binding);
Run time (
dynamic
 binding).
33
Example
List<
Parent
> myObjects =
   new ArrayList<
Parent
>();
myObjects.add(new 
Child1
(
arguments
));
myObjects.add(new 
Child2
(
arguments
));
// add more children of either type...
for (int i = 0; i < myObjects.size(); i++) {
  myObjects.get(i).
aMethod
(
arguments
);
}
Now with abstract Employee class
A
r
r
a
y
L
i
s
t
<
E
m
p
l
o
y
e
e
>
 
e
m
p
l
o
y
e
e
s
 
=
 
n
e
w
 
A
r
r
a
y
L
i
s
t
<
E
m
p
l
o
y
e
e
>
(
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
1
,
 
"
S
n
e
e
z
y
"
,
 
"
i
n
t
e
r
n
"
,
 
1
0
.
0
,
 
4
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
2
,
 
"
D
o
c
"
,
 
"
p
h
y
s
i
c
i
a
n
"
,
 
1
0
0
0
0
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
3
,
 
S
n
o
w
 
W
h
i
t
e
"
,
 
C
E
O
"
,
 
1
7
5
0
0
0
.
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
S
a
l
a
r
i
e
d
E
m
p
l
o
y
e
e
(
4
,
 
"
D
o
p
e
y
"
,
 
"
t
r
a
i
n
e
r
"
,
 
6
5
0
0
0
)
)
;
e
m
p
l
o
y
e
e
s
.
a
d
d
(
n
e
w
 
H
o
u
r
l
y
E
m
p
l
o
y
e
e
(
5
,
 
"
S
l
e
e
p
y
"
,
 
"
t
r
a
i
n
e
e
"
,
 
1
5
.
0
0
,
 
4
0
.
0
0
)
)
;
f
o
r
 
(
i
n
t
 
i
 
=
 
0
;
 
i
 
<
 
e
m
p
l
o
y
e
e
s
.
s
i
z
e
(
)
;
 
i
+
+
)
 
{
 
System.
out.println(employees.get(i));
 
System.
out.println("\tPay: $" + employees.get(i).computePay());
}
This now works because Java knows that every
employee will have a computePay method
.
35
Fredrick P. Brooks 
(1931- )
The Mythical Man-Month
J
o
y
s
 
o
f
 
p
r
o
g
r
a
m
m
i
n
g
We enjoy designing things
because we are created in the
image of God.
The computer is a powerful
and rewarding tool to use.
W
o
e
s
 
o
f
 
p
r
o
g
r
a
m
m
i
n
g
The “mindless” details can be
excessively tedious.
Products become obsolete too
quickly.
As the child delights in his mud pie, so the adult enjoys building things, especially things
of his own design.  I think this delight must be an image of God's delight in making
things, a delight shown in the distinctness and newness of each leaf and each snowflake.
   
         
- F. P. Brooks, Jr.  
The Mythical Man-Month
, 1975
                  images from: http://www.amazon.com/
Slide Note
Embed
Share

Dive into Java object-oriented programming with a focus on implementing inheritance, abstract classes, and polymorphic behavior. Learn the differences between concrete and abstract classes along with practical examples and object modeling insights. Enhance your understanding and skills in Java OOP concepts to unlock the full potential of the language.

  • Java
  • OOP
  • Inheritance
  • Abstract Classes
  • Polymorphism

Uploaded on Feb 18, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. JAVA Object Oriented Programming

  2. Objectives Be able to implement inheritance using extends, super, and abstract Be able to describe differences between a concrete and abstract class. Be able to recognize polymorphic behavior

  3. 3 You can get stuck at the object-based level because you can quickly get there and you get a lot of benefit without much mental effort. It s also easy to feel like you re creating data types you make classes and objects, you send messages to those objects, and everything is nice and neat. But don t be fooled. If you stop here, you re missing out on the greatest part of the language, which is the jump to true object-oriented programming. You can do this only with virtual [abstract] functions. - Bruce Eckel,Thinking in C++,

  4. 4 Example: Analysis Employee We want to make a payroll program that lists each employee, some information about them, and their pay. +myId +myName +myRole +myPayrate +myHours ... Employee e = new Employee(0, Bashful", "customer representative , 10.0, 40.0); System.out.println(e); System.out.println(e.computePay()); ... +toString() +computePay() OUTPUT: 0 Bashful Role: customer representative Payrate: 10.0 Hours Worked: 40.0 Pay: $400.0

  5. 5 Adding More Employee Types Our initial iteration s design assumes that all employees are paid in exactly the same way If we want a different kind of employee, we need to create an entirely new type for it Bashful was an hourly employee, what about a salaried employee? What would be the same? ID, Name, Role What would be different? No mention of hours, salary instead of payRate

  6. 6 Modeling Objects Object-Oriented programming models objects and the relationships between them. Examples: figures, squares, rectangles, polygons, doodles; people, students, teachers, staff members, you; animals, birds, penguins, wings, ; naval vessels, submarines, carriers, fighter jets. image from http://www.linux.org/info/penguin.html

  7. 7 Modeling and Implementing Relationships We have seen two inter-object relationships: is a instantiation implemented using constructor methods (for reference objects); has a aggregation implemented using instance data references; specifies container-contained relationships between classes. The container references the contained. Container Contained +referenceToContained

  8. 8 Modeling and Implementing Relationships Inheritance adds another kind of relationship: implemented using extends clauses;

  9. 9 Class Inheritance Inheritance specifies one-directional, parent-child relationships. is a kind of implemented using extends clause The child inherits the parent s: data methods Each child can polymorphically specialize itself by overriding or adding data or methods. Parent +parent's attributes +parent's operations() Child +parent's attributes +child's attributes +parent's operations() +child's operations()

  10. 10 Carl Linneaus (1707-1778) Taxonomy Systema Naturae, 1758 7 levels: Kingdom Phylum Class Order Family Genus Species images from www.linnean.org & www.kheper.auz.com

  11. 11 Example: Java s Classes All Java classes fit into one hierarchy. All classes inherit from the root class: java.lang.Object You can find the full Java hierarchy here: http://java.sun.com/javase/6/docs/api/overview-tree.html

  12. 12 Example: Design of Employees Employee +myId +myName +myRole +toString() HourlyEmployee SalariedEmployee +myPayrate +myHours +mySalary +computePay() +computePay()

  13. 13 Implementing Inheritance A child class specifies inheritance from a parent class using the extends clause. Java provides three modifiers specifying access for class variables and methods: private protected public It can be safer to declare attributes as private and provide protected accessor and mutator methods.

  14. 14 Super-Class Constructors super is a reference to the parent. A child can invoke its parent s constructor. super(parentConstructorArguments) A call to the parent s constructor: must be the first statement in the constructor; is added automatically if you don t add it. A child can invoke an overridden method: super.parentMethod(arguments)

  15. public class Employee { private int myId; private String myName, myRole; public Employee() { myId = 0; myName = myRole = ""; } public Employee(int id, String name, String role) { myId = id; myName = name; myRole = role; } //accessors and mutators omitted for space @Override public String toString() { return myId + " " + myName + "\n\tRole: " + myRole; } }

  16. public class HourlyEmployee extends Employee { private static final double WORK_WEEK = 40.0, OVERTIME_RATE = 1.5; private double myPayrate, myHours; public HourlyEmployee() { myPayrate = myHours = 0.0; } public HourlyEmployee(int id, String name, String role, double payrate, double hours) { super(id, name, role); myPayrate = payrate; myHours = hours; } //accessor and mutators omitted for space, compute pay on comparison slide @Override public String toString() { return super.toString() + "\n\tPayrate: " + myPayrate + "\n\tHours worked: " + myHours; } }

  17. 17 Example: Design of Employees Employee +myId +myName +myRole +toString() HourlyEmployee SalariedEmployee +myPayrate +myHours +mySalary +computePay() +computePay()

  18. public class SalariedEmployee extends Employee { private double mySalary; public SalariedEmployee() { mySalary = 0.0; } public SalariedEmployee(int id, String name, String role, double salary) { super(id, name, role); mySalary = salary; } //accessors, mutators omitted for space, computePay() on following @Override public String toString() { return super.toString() + "\n\tAnnual Salary: " + mySalary; } }

  19. Comparison of computePay() //Compute pay for hourly employee @Override public double computePay() { if (myHours <= WORK_WEEK) { return myPayrate * myHours; } else { return (myPayrate * WORK_WEEK) + ((myPayrate * OVERTIME_RATE) * (myHours - WORK_WEEK)); } } Hourly Salaried //Compute pay for salaried employee @Override public double computePay() { return mySalary; }

  20. public class PayrollConsole { public static void main(String[] args) { // 1. Employee only Employee e = new Employee(0, Bashful", "customer representative"); System.out.println(e); // 2. HourlyEmployee HourlyEmployee he = new HourlyEmployee(); he.setPayrate(10.0); he.setHours(50.0); System.out.println(he.computePay()); he.setId(1); he.setName("Sneezy"); he.setRole("intern"); System.out.println(he); // 3. SalariedEmployee SalariedEmployee se = new SalariedEmployee(); se.setId(2); se.setName("Doc"); se.setRole("physician"); se.setSalary(100000.0); System.out.println(se); System.out.println(se.computePay()); }}

  21. Fruit Example Fruit Apple Orange VALID OR INVALID?? Fruit fruit = new Gala(); Orange orange = new Orange(); Gala Fuji Orange p = new Apple(); Gala p = new Apple(); Apple p = new Fuji();

  22. Examples public class Test { public static void main(String[] args) { B b = new B(); } } class A { public A(){ } } System.out.println("A's constructor invoked"); class B extends A{ }

  23. Examples public class Test { public static void main(String[] args) { B b = new B(); } } class A { public A(int x){ System.out.println("A's constructor invoked"); } } class B extends A{ public B (){ System.out.println( B s constructor invoked ); } }

  24. class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.doThis()); } } Examples class A { } public A(){ System.out.println("A's constructor invoked"); } public String doThis(){ return A s doThis() ; } class B extends A{ public B (){ } public String doThis(){ return B s doThis() ; } } System.out.println( B s constructor invoked );

  25. Back to our goal We want to be able to print out payroll information: ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(new HourlyEmployee(1, "Sneezy", "intern", 10.0, 40.0)); employees.add(new SalariedEmployee(2, "Doc", "physician", 100000.0)); employees.add(new SalariedEmployee(3, Snow White", CEO", 175000.0)); employees.add(new SalariedEmployee(4, "Dopey", "trainer", 65000)); employees.add(new HourlyEmployee(5, "Sleepy", "trainee", 15.00, 40.00)); for (int i = 0; i < employees.size(); i++) { System.out.println(employees.get(i)); System.out.println("\tPay: $" + employees.get(i).computePay()); } Problem: There is no computePay() for a generic Employee!

  26. 26 Abstract Classes Classes can be abstract or concrete. visibility abstract class className { classDefinition } Like concrete classes, abstract classes: Can have sub-classes; Can implement data and methods. Unlike concrete classes, abstract classes: Cannot be instantiated.

  27. 27 Abstract Methods As with classes, methods can also be declared as abstract or concrete. visibility abstract typemethodName(params); Abstract classes do not provide definitions for their abstract methods. Classes that contain abstract methods must be declared as abstract.

  28. 28 Implementing Inheritance A child class specifies inheritance from a parent class using the extends clause. Concrete sub-classes must define the abstract methods that they inherit. abstract class Parent { public abstract void aMethod(); } Parent +aMethod() class Child1 extends Parent { public void aMethod() { // define method here... } } Child1 +aMethod()

  29. 29 Abstract Class public abstract class Employee { //private instance variables public Employee(int id, String name, String role) { myId = id; myName = name; myRole = role; } public abstract void computePay(); } @Override public String toString() { return myId + " " + myName + "\n\tRole: " + myRole; }

  30. 30 Overriding Methods When an object to asked to execute a method, Java searches up the inheritance hierarchy for a matching method definition. Thus, a sub-class that defines its own version of a method overrides any definitions of the methods that it inherits. A concrete sub-class must implement the abstract methods it inherits using a method with an identical signature.

  31. public class HourlyEmployee extends Employee { private static final double WORK_WEEK = 40.0, OVERTIME_RATE = 1.5; private double myPayrate, myHours; 31 public HourlyEmployee(int id, String name, String role, double payrate, double hours) { super(id, name, role); myPayrate = payrate; myHours = hours; } @Override public double computePay() { if (myHours <= WORK_WEEK) { return myPayrate * myHours; } else { return (myPayrate * WORK_WEEK) + ((myPayrate * OVERTIME_RATE) * (myHours - WORK_WEEK)); } } @Override public String toString() { return super.toString() + "\n\tPayrate: " + myPayrate + "\n\tHours worked: " + myHours; }}

  32. 32 Polymorphism Parent Polymorphism allows different concrete sub-classes to provide potentially different definitions for a given method inherited from their shared parent. Java chooses the appropriate method definition based upon which child the object instantiates at either: Compile time (static binding); Run time (dynamic binding). +aMethod() Child1 Child2 +aMethod() +aMethod()

  33. 33 Example Parent +aMethod() List<Parent> myObjects = new ArrayList<Parent>(); Child1 Child2 +aMethod() +aMethod() myObjects.add(new Child1(arguments)); myObjects.add(new Child2(arguments)); // add more children of either type... for (int i = 0; i < myObjects.size(); i++) { myObjects.get(i).aMethod(arguments); }

  34. Now with abstract Employee class ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(new HourlyEmployee(1, "Sneezy", "intern", 10.0, 40.0)); employees.add(new SalariedEmployee(2, "Doc", "physician", 100000.0)); employees.add(new SalariedEmployee(3, Snow White", CEO", 175000.0)); employees.add(new SalariedEmployee(4, "Dopey", "trainer", 65000)); employees.add(new HourlyEmployee(5, "Sleepy", "trainee", 15.00, 40.00)); for (int i = 0; i < employees.size(); i++) { System.out.println(employees.get(i)); System.out.println("\tPay: $" + employees.get(i).computePay()); } This now works because Java knows that every employee will have a computePay method.

  35. 35 Fredrick P. Brooks (1931- ) The Mythical Man-Month What s the Big Idea Joys of programming Joys of programming We enjoy designing things because we are created in the Woes of programming Woes of programming The mindless details can be excessively tedious. image of God. The computer is a powerful and rewarding tool to use. Products become obsolete too quickly. As the child delights in his mud pie, so the adult enjoys building things, especially things of his own design. I think this delight must be an image of God's delight in making things, a delight shown in the distinctness and newness of each leaf and each snowflake. - F. P. Brooks, Jr. The Mythical Man-Month, 1975 images from: http://www.amazon.com/

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#