Sie sind auf Seite 1von 3

95712C Lecture Notes

Wednesday, September 22, 2004


Packages, Access Specifiers, Composition, Inheritance, Finals, Class Loading
See the Lecture Slides
Packages
Reference: Chapter 4 of Core Jaa, Chapter ! of "hinking in Jaa
Inheritance
Reference: Chapter ! of Core Jaa, Chapters ! # $ of "hinking in Jaa%
Additional ! "erminolo#y
Inheritance
&erriding
Pol'morphism
Access (odifiers
Inheritance
Inheritance is to form class hierarchies )' creating ne* classes from e+isting ones% "he
ne*l' created classes a)sor) the attri)utes and the methods of the e+isting class% "his
*a', inheritance promotes soft*are reusa)ilit' *here tested and de)ugged soft*are is
)eing reused%
A super class and a su) class represent the is,a relationship% A su)class adds its o*n
instance aria)les and instance methods and a su)class refers to a more specific concept
than its superclass%
Overriding
&erriding occurs *hen a su)class redefines a method of its superclass *ith the same
name and *ith the same kinds of parameters%
Polymorphism
An o)-ect reference can refer to o)-ects of )ase class as *ell as to o)-ects of an' su)class
in the inheritance hierarch'%
Access Modifiers
http:..-aa%sun%com.docs.)ooks.tutorial.-aa.-aa&&.accesscontrol%html
Specifier class subclass package world
private
/
protected
/ / /
public
/ / / /
package
/ /
$n%eritance &ec%anism
Let us see ho* the inheritance *orks%
Base vs Derived lasses
0ase Class 1eried Class
Super class 2 a superset Su)class 2 a su)set
Parent class Child class
3eneral concept Special concept
Less functionalit' (ore functionalit'
Protected vs Private Members of the Base lass
Protected instance aria)les are inherited and can )e accessed )' the deried class%
Priate instance aria)les are inherited )ut cannot )e accessed% 4ae to use the pu)lic
API of the )ase class to access them%
!he super keyword
"he super ke'*ord allo*s inoking the constructors and the methods of the )ase class%
5nlike the this ke'*ord, it is not a reference%
In constructors, the su)classes cannot access the priate mem)ers of the )ase classes%
4o*eer, the' need to )e initiali6ed% "he super ke'*ord is used to initiali6e the instance
mem)ers inherited from the )ase class% "he first sentence could )e a super in a su)class%
Remem)er that it could )e this as *ell% 7ith this, *e are inoking another constructor
oerloaded )' our class%
Declared !ype vs Actual !ype
1eclared t'pe indicates *hat methods can )e inoked% &nl' the methods defined in the
declared t'pe can )e inoked on an o)-ect%
Overriding vs Overloading
&erriding occurs in an inheritance hierarch' )' redefining a method *ith the same kinds
of parameters% &erloading occurs *ithin the same class definition and )' redefining a
method *ith the same name )ut *ith different parameters%
"#amples
See (anager"est%-aa
See Person"est%-aa
Eclipse features
Packages

Das könnte Ihnen auch gefallen