Föreläsning 14 Föreläsning 14 Dagens föreläsning: arv

!
"#
$
&
' !
!
! #
! !
)
! "
#
!
*
"
#
+
"
#
*
,
.
%
!
!
((
!
*
"
#
+
"
#
*
,
!
.
!
*
#
0
Person/
&
* )
Person
!
&
Student
Student
1
%
!
*
!
Student/
#
!
Person/
class Student extends Person {
}
2
* )
1
Student
Person
!
)
)
1
!
*
"
#
+
"
#
*
,
!
.
!/
5
3
Djur
!
!
Hund
class Hund extends Djur {
444
}
Djur
Hund
Hund
Djur
' !
)
!
!/
*
3 )
!
)
)
!6
)
public static void main(String[] args) {
Student nora = new Student(”Nora”);
Std.out.println(nora.geNamn());
}
geNamn
Person
"
Student !
geNamn !
Student
Person
!/
&
3 )
7
$
4 4 4 super 8
!
' ! 9
class Person {
public Person(String namn, int alder) {
444
}
}
class Student extends Person {
public Student(String n, int a, int p) {
super(n, a);
poang = p;
}
}
!/
:!
3 )
3 )
&
!
class Person {
public void skrivUt() {
444
}
class Student extends Person {
public void skrivUt() {
444
}
!
#
!/
!
&
!
)
super
4 4 4 super
)1
!
class Person {
public void skrivUt() {
444
}
class Student extends Person {
public void skrivUt() {
super.skrivUt();
444
}
"
$
#
!/
5
class Student extends Person {
444
}
Student s = new Student();
()1
!
Student
)1
!
super Student
; )
;
Person/ )1
Person
%
!/
public protected
private
3 )
)
!
protected
public
private
&
%
private protected public
'
'
'
'
'
'
'
!
*
"
#
+
"
#
*
,
.
!
!
'
'
'
)
!/ #
< ))
*
1
))
<)
*
)
)
!
(
1
)
.
*
!
)
!/ #
< ))
(
)
*
" Verk
" Bok
" Film
*
Verk Bok
Film
!
!
Verk
)
)
!
*
"
#
+
"
#
*
,
.
!
4
!
,
!
*
*
!
6
.
!
1
;
;
3
)
!
3
(
)
,
$
5 6
()1
!
!
!
$
!
,
$
5 6
!
!
=
"
)1
1 !
1
()1
3
!
>
1
!
!
!
>
!
!
!
,
!
!
$
5 6
!
=
=0
"
!?)
!
"#
3
A
1
.
(
1
@
<
<
1
1
7
!
*
"
#
+
"
#
*
,
!
.
! )
5
8
9
)
"#
"
" )
&
"#
"
"
C 8
B
)
!9
"#
"
" )
!
B
5
8
9
)
"#
"
" )
1
" Rektangel
' !
! Horn
!
)
class Person {
private String namn;
private String adress;
private int alder;
}
Person
&
"#
"
B
"
)
! 1
' !
!
)
class Person {
private Person makeMaka;
}
Person
B
class Lantagare extends Person {
private Bok[] bocker;
}
Lantagare
)
C 8 !9
4!4 4
"#
"
" 3+3/
"
<
(
+
!
C/
)
class Person {
private String namn;
public String geNamn() {
return namn;
}
}
C/
)
class Person {
private String namn;
public String geNamn() {
return namn;
}
}
class Larare
extends Person {
private int rumsNr;
public int geRumsNr() {
return rumsNr;
}
}
C/
)
class Person {
private String namn;
public String geNamn() {
return namn;
}
}
class Larare
extends Person {
private int rumsNr;
public int geRumsNr() {
return rumsNr;
}
}
class Student
extends Person {
private int poang;
public int gePoang() {
return poang;
}
}
C/
)
class Person {
private String namn;
public String geNamn() {
return namn;
}
}
class Student
extends Person {
private int poang;
public int gePoang() {
return poang;
}
}
class Larare
extends Person {
private int rumsNr;
public int geRumsNr() {
return rumsNr;
}
}
class STSStudent
extends Student {
private int labGrupp;
public int geLabGrupp() {
return labGrupp;
}
}
!
*
"
#
+
"
#
*
,
!
.
.
"#
<)
&
)
!
VerkRegister
4"
!
!
class VerkRegister {
private Verk[] register;
private int antalVerk;
public void laggInVerk(Verk verk) {
register[antalVerk] = verk;
antalVerk++;
}
}
&
)
6
Verk
Verk
.
"#
class VerkRegister {
private Verk[] register;
private int antalVerk;
public void laggInVerk(Verk verk) {
register[antalVerk] = verk;
antalVerk++;
}
}
$
)
!
%
7
&
)
?
Verk
Verk
?
.
+
(
!
Hund
)
)1
"
Djur
Hund
!
!
Hund
Djur
Djur
1
(
"
)
!
!
!
)
)1
2+"
1
+
!
Djur
Hund
!/
&
()1
' !
!
Object
;
;
5
8
!
!
' !
' !
4 4 DEEE
! )
java.lang
$
java.util
8
java.io
4<4 4
3
;
!
;
9
!
http://java.sun.com/javase/6/docs/api/
8
$
9
' !
.
String
Math
java.util.Vector
#
java.util.Arrays
! )1
!
javax.swing.*
!
9
3
1 !
' !
&
"
)1
!
! )1
43
8
1
%
Vector
Vector
!
1 ! 4
!
&
import java.util.Vector;
+
F!
G
*
Vector<String> listaAvStrangar;
listaAvStrangar = new Vector<String>();
F!
G
!
' ! 4*
' !
&
Vector
!
Vector<E>
void add(E element)
E remove(int index)
E get(int index)
int size()
*
1
%