More on super (1/1)
1 c l a s s S t u d e n t {
2 i n t ID ; S t r i n g name ;
3 v o i d s t u d y _ f o r _ m i d t e r m ( ) {
4 System . o u t . p r i n t l n ( " I am s t u d y i n g f o r midterm . " ) ;
5 }
6 }
7 c l a s s AwardStudent extends S t u d e n t {
8 Award [ ] p r e s i d e n t _ a w a r d s ;
9 v o i d s t u d y _ f o r _ m i d t e r m ( ) {
10 f o r(i n t i = 0 ; i < 1 0 ; i ++)
11 super. s t u d y _ f o r _ m i d t e r m ( ) ;
12 }
13 }
super: much like (ParentName)this (but NOT the same)
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 27 / 35
More on super: Key Point
this: a Sub-type reference variable pointing to the object itself super: a Base-type reference variable pointing to the object itself
same reference value, different type super.super: no
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 28 / 35
Inheritance (1/5)
1 c l a s s P r o f e s s o r { }
2 c l a s s CSIEProfessor extends P r o f e s s o r { }
3 c l a s s EEProfessor extends P r o f e s s o r { }
4
5 c l a s s Demo{
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
7 CSIEProfessor HTLin = new CSIEProfessor ( ) ;
8 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;
9 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f CSIEProfessor ) ;
10 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f P r o f e s s o r ) ;
11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f EEProfessor ) ;
12 }
13 }
HTLin is an instance of CSIEProfessor
HTLin is an instance of Professor (because CSIEProfessor is a type of Professor)
HTLin is not an instance of EE Professor [the compiler knows]
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 29 / 35
Inheritance (2/5)
1 c l a s s P r o f e s s o r { }
2 c l a s s CSIEProfessor extends P r o f e s s o r { }
3 c l a s s EEProfessor extends P r o f e s s o r { }
4
5 c l a s s Demo{
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
7 P r o f e s s o r HTLin = new CSIEProfessor ( ) ;
8 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;
9 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f CSIEProfessor ) ;
10 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f P r o f e s s o r ) ;
11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f EEProfessor ) ;
12 }
13 }
HTLin is an instance of CSIEProfessor HTLin is an instance of Professor
HTLin is not an instance of EE Professor [not easily determined at compile-time, but can be checked at run-time]
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 30 / 35
Inheritance (3/5)
1 c l a s s P r o f e s s o r { }
2 c l a s s CSIEProfessor extends P r o f e s s o r { }
3 c l a s s EEProfessor extends P r o f e s s o r { }
4
5 c l a s s Demo{
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
7 P r o f e s s o r HTLin = new CSIEProfessor ( ) ;
8 i n t s co re = 100;
9 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;
10 System . o u t . p r i n t l n ( sc or e . g e t C l a s s ( ) ) ;
11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f j a v a . l a n g . O b j e c t ) ;
12 System . o u t . p r i n t l n ( sc or e i n s t a n c e o f j a v a . l a n g . O b j e c t ) ;
13 }
14 }
every valid object is an instance of java.lang.Object
primitive type is not an instance of anything [easily determined at compile-time]
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 31 / 35
Inheritance (4/5)
1 c l a s s P r o f e s s o r { }
2 c l a s s CSIEProfessor extends P r o f e s s o r { }
3 c l a s s EEProfessor extends P r o f e s s o r { }
4
5 c l a s s Demo{
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
7 P r o f e s s o r [ ] p a r r = new CSIEProfessor [ 3 ] ;
8 System . o u t . p r i n t l n ( p a r r . g e t C l a s s ( ) ) ;
9 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f O b j e c t ) ;
10 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f O b j e c t [ ] ) ;
11 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f P r o f e s s o r [ ] ) ;
12 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f CSIEProfessor [ ] ) ;
13 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f EEProfessor [ ] ) ;
14 }
15 }
every object array is an instance of Object[]
Object[] is a reference type
every reference type “extends” Object every object array is an instance of Object
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 32 / 35
Inheritance (5/5)
1 c l a s s Demo{
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
3 i n t[ ] o a r r = new i n t[ 3 ] ;
4 System . o u t . p r i n t l n ( o a r r . g e t C l a s s ( ) ) ;
5 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f O b j e c t ) ;
6 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f O b j e c t [ ] ) ;
7 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f double[ ] ) ;
8 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f s h o r t[ ] ) ;
9 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f i n t[ ] ) ;
10 }
11 }
a int array is an instance of int[]
int[] is a reference type
every reference type “extends” Object every object array is an instance of Object
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 33 / 35
Inheritance: Key Point
compile-time detectable: only siblings (int sibling of Object, EEProfessor sibling of CSIEProfessor, etc.)
run-time detectable: other instances (or null)
1 CSIEProfessor SomeOne = new CSIEProfessor ( ) ;
2 P r o f e s s o r p ;
3 p = SomeOne ; / / note : r e f e r e n c e assignment
4 i f ( p i n s t a n c e o f CSIEProfessor && p ! = n u l l) {
5 CSIEProfessor pCSIE = ( CSIEProfessor ) p ;
6 / / . . .
7 }
8 / ∗ i n s t a n c e o f i s n o t r e a l l y used v e r y o f t e n ;
9 b u t u s e f u l i n enhancing u n d e r s t a n d i n g . ∗ /
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 34 / 35
Java Type Hierarchy
H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 35 / 35