Inheritance in Java
Inheritance in Java
bark
() void
Amimal
& extends Dog class
sound"); some println
(" out-System.
sound() void
Animal class
Example
superclass. one rom inherits class "A
Inheritence Single )
Java: nherilence
in ofJupes 1
programmer uriOus-
classes.
relationship establishes reusability
and
Code promotes It
class).
It parent class/ (super
class existing Cmethods)
o behaviours and
es the acquires an class) lchild(subclassl class
new where
a mechanism a isjava nheritence
in
INHERITANCE
JAVA IN
Page :
cIASSMAte Date:
("Playing
: ") printhn out Systern.
()play
Dog extends PupPy yoid class
("Barking")i println outSystem.
C)
bark Yoid
Animal extcnds DoQ class
sound"): ("some printlnSystem.out
(0e sound void
Amimal class
Example
subclass). already isthat
class afrom inherits class a class
derived another trorm derived isi-e,class A
Inheritence Multilevel b)
Page:
Date
cIASSMAte
class Class 3 Exarmple baseclasscanme " c)
meouw
Void () bark
yoidOe Amimalclass Mutiple
soundVoid)E Hier
System.Systm.out-printhm System.
("Backing") :
Cat Dog class archical
out extends out- classes
extends tor
@ printn
ious printhn SerVe
Animal Inheritence:
îrnherit
Animal several
-
("Meowing"); ("some
er { i as ron
sound"); child
the
classes. a
parenit
superclass.
single Page: Date
clASSMAte
class
or
clASSMAte
Date
Page
Parent class
Properties 0004 Properies and behavior
Behavior 000+ Spec ific to parent
J lmheritence
aheritel proprkes D Propertes and beh avior
Inherited behavior OO specific to parent
child class
@ urious- proqrammer
mherttence
Employee Super class Sstudent Super class
Inherts Inherits
Maçks. subelass /
Bxeeutive Subclass. Inherits intoimdiate
class. base
Sinole Inheritence
Sporks derived class
Mulilevel inheritence
Hierorchical inheritente
class method
"):
clASSMAte
parent
sound superclass
DatePage constructors.
Superclass
immediate makes ")i
methods.
Superclass barks urious-
progranmme
lcalling
object. ("Animal "Dog
the
i print(
Animal ()
to
Keylordrert ()E
pzintlm
soumd
void f sound
refers
cuY ()
extends
sOund
Super.
Amimal
class Systemout SysBem-out
:IH the 2-
Accessing
1.Accessing
2.
Super of
"Use
Case
s: Dog
"Puxpose Example void
class
subclass
implemerntation
f
a be
its
parameters ")
cIASSMAte not
in sOLnd
a must programmer
detined barks");
:Page
Date
allous makes
retn
type).superclass
(name private.
already
overriding ("Animal ("Dog
-
Own signature urious E
Overriding Animal printn
is the printn
to
provide
its
Method Hhat @ sound
o
void
Superclass method in static.
tinal out: voidout-
extends()
sound
method "Conditions 2. class Systcm.
hemethod Animal @ System.
Override
"Definitian: :
3.
Method Dog
Sarme Example
class 3
1:
automatically
cIASSMAte
incorrectly. Casting
a and Doum
Page:
a to
Downcasking:to happenis
Date
Upcasting programm
reference
casting l
reference done Doa()i
animal
casting
Implicit if
explicit
superclass
Subclass ewanimal
C
dogidog new
-
Exception (Dog)
uriouS
and =
requires =
Upcastine animal @
a Cast
Upcasting "Douncasting
Converting doeDog Class
Canverting dog
Example:Animal
reference reference
It Example:
Animal
Dog
4 to
cIASSMAte
Date
Page:
Example