Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture Notes
Inheritance
Inheritance: Definition
Inheritance terms
Inheritance in Java
the objects of your class will now receive all of the state
(fields) and behavior (methods) of the parent class
constructors and static methods/fields are not inherited
by default, a class's parent is Object
Inheritance Example
class BankAccount {
private double myBal;
public BankAccount() { myBal = 0; }
public double getBalance() { return myBal; }
}
class CheckingAccount extends BankAccount {
private double myInterest;
public CheckingAccount(double interest) { }
public double getInterest() { return myInterest; }
public void applyInterest() { }
}
Multiple layers of
inheritance
it is possible to extend a class that itself is a
Inheritance Hierarchies
"Has-a" Relationships
"Is-a" relationships
10
Mixing inheritance,
interfaces
It is legal for a class to extend a parent and to implement any
number of interfaces
12
Overriding behavior
13
Overriding behavior
example
BankAccount b = new BankAccount("Ed",
9.0);
FeeAccount f = new FeeAccount("Jen", 9.0);
System.out.println(b);
System.out.println(f);
Output:
Ed $9.0
Jen $9.0 (Fee: $2.0)
14
an industry-standard way
to draw pictures of your
classes and their
relationships to each other
classes are boxes that list
the fields, methods, and
constructors of the type
classes that have
inheritance relationships,
or are tightly related to
each other, are connected
by arrows
15
- for private
+ for public
# for protected
example: - mySize: int
16
inheritance relationships
17
Class diagram:
associations
associational relationships
1. multiplicity (how many)
2. name
(what relationship the objects
have)
18
19
Access modifiers
//
//
//
//
//
//
//
//
23
super keyword
syntax:
super(args);
super.fieldName
super.methodName(args);
24
super example
public class BankAccount {
private double myBalance;
public void withdraw(double amount) {
myBalance -= amount;
} }
public class FeeAccount
extends BankAccount {
public void withdraw(double amount) {
super.withdraw(amount);
if (getBalance() < 100.00)
withdraw(2.00); // charge $2 fee
} }
25
26
Example:
Object o = myList.get(2);
if (o instanceof BankAccount)
((BankAccount)o).deposit(10.0);
27
Some instanceof
problems
Object o = new BankAccount(...);
BankAccount c = new CheckingAccount(...);
BankAccount n = new NumberedAccount(...);
CheckingAccount c2 = null;
T/F ???
o instanceof Object
_______
o instanceof BankAccount
_______
o instanceof CheckingAccount
_______
c instanceof BankAccount
_______
c instanceof CheckingAccount
_______
n instanceof CheckingAccount
_______
n instanceof Comparable
_______
n instanceof NumberedAccount
_______
c2 instanceof Object
_______
28
static methods
referring to instance variables
the types of the reference variables you declare
Polymorphism
((BankAccount)o).withdraw(5.00);
34
((String)o).toUpperCase();
((FeeAccount)o).withdraw(5.00);
// crashes
// crashes
35
A dynamic binding
problem
class A {
public void method1() { System.out.println(A1); }
public void method3() { System.out.println(A3); }
}
class B extends A {
public void method2() { System.out.println(B2); }
public void method3() { System.out.println(B3); }
}
A var1 = new B();
Object var2 = new A();
var1.method1();
var1.method2();
var2.method1();
((A)var2).method3();
OUTPUT???
36
A bad solution
public class Shape implements Shape2D {
private int myX, myY;
public Shape(int x, int y) {
myX = x; myY = y;
}
public int getX() { return myX; }
public int getY() { return myY; }
38
Abstract classes
41
Extending an abstract
class
public class Rectangle extends Shape {
private int myWidth, myHeight;
42
43
Questions, Practice
What are the differences between
problem
44
Inner classes
usefulness:
46
47
References