0% found this document useful (0 votes)
7 views

Inheritance in Java

The document discusses inheritance in Java, explaining concepts such as single, multilevel, and hierarchical inheritance. It highlights the importance of reusability and how subclasses inherit properties and behaviors from parent classes. Additionally, it covers the final keyword, which prevents classes from being subclassed and methods from being overridden.

Uploaded by

Pakeeza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Inheritance in Java

The document discusses inheritance in Java, explaining concepts such as single, multilevel, and hierarchical inheritance. It highlights the importance of reusability and how subclasses inherit properties and behaviors from parent classes. Additionally, it covers the final keyword, which prevents classes from being subclassed and methods from being overridden.

Uploaded by

Pakeeza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

("Barking")i primtlm out System

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

Student Super class


Inherits Inherits

Seience Commercel LArts


Subclasses

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:

5. Final Kesword in Inheritance:


1 tinal dass:
class declared woith final cannot be subclassed.
It Cannot be extended by any other
class
Ihis means we cannot creae Subclass f
a final class , ensuring its behavioY is not
altered through inheritance.

Example

tinal class Animal


Il cannot be inherited
Void sOUnd c)E
System. out - printhn ("Animal makes sound");

class Dog extends Animal &E


lCompile time error
Il This will throw a compilaticn error because Animal
is inal and cannot be subclassed.
@urioUs- programmer
"); the
because
mnethod
is
itwhere
cIASSMAte Sound in
be tinal
error
DatePage
cannot the makes
methoddeclared ;barks')@urious
-programmer
of ("Animal tme
final cf
behavior -pile
regardless C sOuTd) "Dog
is
as c) printtn AnimalcoYn()
sound (
2 declared
method
A
method:
inal the a soumd
that
, override
throw
extends print
same
subclasses out clas.
Aninmal
void
Animal
class System methodC)
sound
Void
--out
ensures will
theinherited.
Example inal Dogcannot
System
IL the
This
Stays classlIl
by lhis
values igned
Constant
throughout
the
clASSMAte reass
define
towant cannot
be variable
'age'
Date Pago
cannot proorammer
error:
changed. age
l -ime
program final
you
when
i compile -
cannot
be 5 urious
final
3.variable:
is
that
should
remain void to
age C)
changeAQe
the yalue
tinal
A
yariable C
isThis
useful int
o class
Dog =6
its
value Eampleinal
execution lage

You might also like