You are on page 1of 16

ClS 304

Cb[ecL ConcepLs and 1ermlnology - 2



ur. SLeven Curl
rofessor, CompuLer lnformauon SysLems
Cal oly omona
1odays 1oplcs
- lnherlLance
- Superclasses, Subclasses, and olymorphlsm
- MeLa lnformauon and Casung
- AbsLracL and llnal keywords


lnLroducuon Lo lnherlLance

lnherlLance
Allows Lhe creauon/modlcauon of a class uslng an exlsung class !"#$%&#
()"*+, #$( %-","+./ 0%)(1 A class LhaL lnherlLs from an exlsung class ls sald
Lo (2#(+) Lhe parenL class. names for Lhe new class lnclude )(-"3()4 0$"/)4
%- 5&60/.551 names for Lhe exlsung class lnclude 6.5( 0/.554 7.-(+# 0/.554
%- 5&7(-0/.551

SynLax:
exLends 0/.55+.8(

Lxamples:
class 8ook exLends roducL, class Car exLends vehlcle

lnLroducuon Lo lnherlLance

AdvanLages
- llelds, consLrucLors, and meLhods are auLomaucally passed Lo Lhe
chlld class wlLhouL copylng and pasung (l.e., dupllcaung) code
- Speeds developmenL
- lmproves quallLy by reduclng Lhe need for Lesung
- Allows changes only where necessary
- lmproves code reusablllLy by exLendlng , noL edlung, parenL code.
updaLes Lo Lhe parenL wlll auLomaucally pass Lhrough Lo Lhe chlld
classes aer recomplllng.
lnLroducuon Lo lnherlLance

LxLendlng a arenL Class
A subclass can exLend lLs parenL class by addlng new elds, consLrucLors,
and meLhods. Crlglnal meLhods can be %3(--"))(+ by lncludlng a new
verslon ln Lhe subclass.

A few noLes abouL %3(--")"+, a parenL class:
- 1he new meLhod should have Lhe same 5",+.#&-(
- ConsLrucLor requlremenLs are sull malnLalned
- 8oLh Lhe orlglnal and overrldden meLhods are always avallable
- 1o access Lhe orlglnal meLhod, use Lhe keyword 5&7(-1


lnLroducuon Lo lnherlLance

1he Cb[ecL Class
1he parenL class of all ob[ecLs ls Lhe 96:(0# class.

MeLhods of Lhe 96:(0# class:
- LoSLrlng( ) 8eLurns a SLrlng ob[ecL conLalnlng Lhe class name
- equals( ) 8eLurns Lrue lf Lwo ob[ecLs occupy Lhe same
memory space
- geLClass( ) 8eLurns a Class ob[ecL of Lhe same Lype
- clone( ) 8eLurns a copy of Lhls ob[ecL
- hashCode( ) 8eLurns Lhe hashcode for Lhls ob[ecL
- nallze( ) Called by Lhe garbage collecLor when no more
references exlsL.


Superclasses, Subclasses, and olymorphlsm

Access Modlers

1ype urpose
- publlc Avallable everywhere
- prlvaLe Avallable wlLhln Lhe currenL class only
- proLecLed Avallable wlLhln Lhe currenL package and by
lnherlLance
- package (defaulL) Avallable wlLhln Lhe currenL package
A subclass can always access Lhe publlc and proLecLed members of lLs
parenL class, someumes package, buL noL prlvaLe members.

Superclasses, Subclasses, and olymorphlsm

Creaung a Superclass (arenL)

publlc roducL( )
[
prlvaLe SLrlng code, name, descrlpuon,
roducL( )
[ code, name, descrlpuon = , }

publlc SLrlng LoSLrlng( )
[
reLurn = name: + name + \n +
uescrlpuon: + descrlpuon + \n,
}
}
Superclasses, Subclasses, and olymorphlsm

Creaung a Subclass (Chlld)

publlc 8ook( ) exLends roducL
[
prlvaLe SLrlng auLhor, sub[ecL,
8ook( )
[ super( ), }

publlc SLrlng LoSLrlng( )
[
reLurn = super.LoSLrlng( ) +
AuLhor: + auLhor + \n + Sub[ecL: + sub[ecL,
}
}
Superclasses, Subclasses, and olymorphlsm

olymorphlsm
1reaung ob[ecLs of dlerenL Lypes as lf Lhey were Lhe same by referrlng Lo
a superclass LhaL ls common Lo all Lhese ob[ecLs

A few noLes abouL 7%/;8%-7$"58:
- Allows a common lnLerface even when ob[ecLs are of varled Lypes
- Subclasses can be LreaLed as lf Lhey were lnsLances of Lhe parenL class,
even when Lhey are noL
- Cenerlc code of Lhe parenL class can be shared and exLended Lo lnclude
deLalled behavlor of Lhe chlld classes
- lf you have Lo know Lhe class Lype before lmplemenL a behavlor, lLs noL
polymorphlsm



MeLa lnformauon and Casung

lnformauon abouL an Cb[ecLs 1ype
!ava creaLes a generlc Class ob[ecL Lo Lrack all ob[ecLs LhaL are loaded lnLo
memory.

lrequenLly used meLhods for class Lypes:

- geLClass( ) 8eLurns a Class ob[ecL wlLh lnformauon
abouL Lhe ob[ecL belng referenced.

- geLname( ) 8eLurns Lhe name of Lhe class
- lnsLanceCf( ) 8eLurns Lrue lf an ob[ecL ls an lnsLance of
a parucular class


MeLa lnformauon and Casung

Casung Cb[ecLs
- Casung from a subclass Lo lLs superclass ls done lmpllclLly. lor example,
a subclass reference wlll always work whenever Lhe superclass ls
needed.

- Casung from a superclass Lo lLs subclass musL be done expllclLly.
- Casung aecLs whlch meLhods are avallable from a glven ob[ecL. SLorlng
a subclass ln a varlable deslgned for lLs superclass wlll prevenL access Lo
meLhods ln Lhe subclass unless expllclLly casL.


AbsLracL and llnal keywords

absLracL
An absLracL class ls lncompleLe and Lherefore cannoL be lnsLanuaLed.
AbsLracL classes can only be used by creaung subclasses from lL and Lhen
creaung ob[ecLs from Lhose subclasses.

- AbsLracL classes are creaLed uslng Lhe .65#-.0# keyword.
- AbsLracL classes may conLaln elds, consLrucLors, and meLhods [usL llke any
oLher class buL may also conLaln .65#-.0# meLhods.
- AbsLracL 8(#$%)5 cannoL have a meLhod body [ } and musL be overrldden ln
Lhe subclass.


AbsLracL and llnal keywords

nal
Marklng a class, meLhod, or eld as <+./ prevenLs oLhers from uslng
lnherlLance Lo overrlde or oLherwlse change Lhese lLems.

- 1he keyword <+./ as a deslgn Lool prevenLs oLhers from changlng ones
work.
- lLems marked as <+./ wlll run sllghLly fasLer Lhan oLher slmllar lLems.
- Marklng a class as <+./ auLomaucally makes all meLhod ls Lhe class nal as
well.


Cuesuons
1ry Lhe revlew exerclses
ln 8lackboard.

You might also like