Hsuan-Tien Lin
Deptartment of CSIE, NTU
OOP Class, April 14, 2009
Instance Variables and Inheritance (1/3)
1 c l a s s P r o f e s s o r {
2 S t r i n g t i t l e ;
3 P r o f e s s o r ( ) { t i t l e = "NTU" ; }
4 p u b l i c S t r i n g g e t _ t i t l e ( ) { r e t u r n t i t l e ; }
5 }
6 c l a s s CSIEProfessor extends P r o f e s s o r {
7 S t r i n g t i t l e ; / / what?
8 CSIEProfessor ( ) { t i t l e = " CSIE " ; / / which t i t l e ? }
9 p u b l i c S t r i n g g e t _ c s i e _ t i t l e ( ) { r e t u r n t i t l e ; / / which t i t l e ? }
10 }
11
12 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;
13 System . o u t . p r i n t l n ( HTLin1 . g e t _ t i t l e ( ) ) ; / / NTU
14 System . o u t . p r i n t l n ( HTLin1 . g e t _ c s i e _ t i t l e ( ) ) ; / / CSIE
15 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;
16 System . o u t . p r i n t l n ( HTLin2 . g e t _ t i t l e ( ) ) ; / / NTU
17 System . o u t . p r i n t l n ( HTLin2 . g e t _ c s i e _ t i t l e ( ) ) ; / / hahaha
18 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;
19 System . o u t . p r i n t l n ( HTLin3 . g e t _ t i t l e ( ) ) ; / / NTU
20 System . o u t . p r i n t l n ( HTLin3 . g e t _ c s i e _ t i t l e ( ) ) ; / / hahaha
What TYPE-of-reference wants to access its title?
1 c l a s s P r o f e s s o r {
2 S t r i n g t i t l e ; / ∗ P r o f e s s o r ( t h i s ) ∗ /
3 P r o f e s s o r ( ) { t i t l e = "NTU" ; } / ∗ P r o f e s s o r ( t h i s ) ∗ /
4 p u b l i c S t r i n g g e t _ t i t l e ( ) { r e t u r n t i t l e ; }
5 / ∗ P r o f e s s o r ( t h i s ) ∗ /
6 }
7 c l a s s CSIEProfessor extends P r o f e s s o r {
8 S t r i n g t i t l e ; / ∗ CSIEProfessor ( t h i s ) ∗ /
9 CSIEProfessor ( ) { t i t l e = " CSIE " ; } / ∗ CSIEProfessor ( t h i s ) ∗ /
10 p u b l i c S t r i n g g e t _ c s i e _ t i t l e ( ) { r e t u r n t i t l e ; }
11 / ∗ CSIEProfessor ( t h i s ) ∗ /
12 p u b l i c S t r i n g g e t _ s u p e r _ t i t l e ( ) { r e t u r n super. t i t l e ; }
13 / ∗ P r o f e s s o r ( super ) ∗ /
14 p u b l i c S t r i n g g e t _ s u p e r _ t i t l e _ 2 ( ) {
15 r e t u r n ( ( P r o f e s s o r )t h i s) . t i t l e ;
16 }
17 / ∗ P r o f e s s o r ( t h i s as P r o f e s s o r ) ∗ /
18 }
Instance Variables and Inheritance (3/3)
What TYPE-of-reference wants to access its title?
1 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;
2 System . o u t . p r i n t l n ( HTLin1 . t i t l e ) ; / ∗ CSIEProfessor ( HTLin1 ) ∗ /
3 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;
4 System . o u t . p r i n t l n ( HTLin2 . t i t l e ) ; / ∗ P r o f e s s o r ( HTLin2 ) ∗ /
5 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;
6 System . o u t . p r i n t l n ( HTLin3 . t i t l e ) ; / ∗ P r o f e s s o r ( HTLin3 ) ∗ /
7 CSIEProfessor HTLin4 = new CSIEProfessor ( ) ;
8 System . o u t . p r i n t l n ( ( ( P r o f e s s o r ) HTLin4 ) . t i t l e ) ;
9 / ∗ P r o f e s s o r ( HTLin4 as P r o f e s s o r ) ∗ /
compile-time (static) binding: compiler easily decide intention by declared type
instance variables: static binding, new variable w/ same name
“hides” old one (when using new type)
1 c l a s s P r o f e s s o r {
2 s t a t i c i n t cou nt ;
3 P r o f e s s o r ( ) { c oun t ++; } / / i . e . , P r o f e s s o r . cou nt ++;
4 }
5 c l a s s CSIEProfessor extends P r o f e s s o r {
6 s t a t i c i n t cou nt ;
7 CSIEProfessor ( ) {
8 super( ) ; c oun t ++; / / i . e . , CSIEProfessor . c oun t ++;
9 }
10 }
11 System . o u t . p r i n t l n ( CSIEProfessor . cou nt ) ; / / c l e a r i n t e n t i o n
12 System . o u t . p r i n t l n ( P r o f e s s o r . c oun t ) ; / / c l e a r i n t e n t i o n
13 / / i n t e n t i o n determined by r e f e r e n c e TYPE
14 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;
15 System . o u t . p r i n t l n ( HTLin1 . c oun t ) ; / / CSIEProfessor . cou nt
16 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;
17 System . o u t . p r i n t l n ( HTLin2 . c oun t ) ; / / P r o f e s s o r . cou nt
18 P r o f e s s o r HTLin3 = n u l l;
19 System . o u t . p r i n t l n ( HTLin3 . c oun t ) ; / / P r o f e s s o r . cou nt
class variables: static binding (as said before)
Class Methods and Inheritance (0/0)
class methods: exactly the same as class variables
intention determined by directly using class names, or by
reference TYPE
1 c l a s s P r o f e s s o r {
2 S t r i n g s k i l l = " t e l l j o k e s " ;
3 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l ; }
4 }
5 c l a s s J a i J a i P r o f e s s o r extends P r o f e s s o r {
6 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l + " , p l a y on BBS" ; }
7 }
8
9 J a i J a i P r o f e s s o r HTLin1 = new J a i J a i P r o f e s s o r ( ) ;
10 System . o u t . p r i n t l n ( HTLin1 . g e t _ s k i l l ( ) ) ;
11 System . o u t . p r i n t l n ( ( ( P r o f e s s o r ) HTLin1 ) . g e t _ s k i l l ( ) ) ;
12 P r o f e s s o r HTLin2 = new J a i J a i P r o f e s s o r ( ) ;
13 System . o u t . p r i n t l n ( HTLin2 . g e t _ s k i l l ( ) ) ;
14 System . o u t . p r i n t l n ( ( ( J a i J a i P r o f e s s o r ) HTLin2 ) . g e t _ s k i l l ( ) ) ;
15 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;
16 System . o u t . p r i n t l n ( HTLin3 . g e t _ s k i l l ( ) ) ;
17 System . o u t . p r i n t l n (
18 ( ( J a i J a i P r o f e s s o r ) HTLin3 ) . g e t _ s k i l l ( )
19 ) ;/ / ohohoh , why do you want t o be J a i J a i ?
Instance Methods and Inheritance (2/2)
1 c l a s s P r o f e s s o r {
2 p r i v a t e S t r i n g s k i l l = " t e l l j o k e s " ;
3 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l ; }
4 p u b l i c S t r i n g g e t _ s k i l l _ o h o h o h ( ) { r e t u r n s k i l l ; }
5 }
6 c l a s s J a i J a i P r o f e s s o r extends P r o f e s s o r {
7 p u b l i c S t r i n g g e t _ s k i l l _ h a h a h a ( ) { r e t u r n s k i l l + " , p l a y on BBS" ; }
8 p u b l i c S t r i n g g e t _ s k i l l _ o h o h o h ( ) {
9 r e t u r n g e t _ s k i l l _ o h o h o h ( ) + " , p l a y on BBS" ;
10 }
11 p u b l i c S t r i n g g e t _ s k i l l ( ) {
12 r e t u r n super. g e t _ s k i l l ( ) + " , p l a y on BBS" ;
13 }
14 }