-
Experiment No. 5
Objective: Create Java Codes using inheritance and polymorphis
m
a. Singl e Inheritance
b. Multilevel Inheritance
c. Hierarchical Inheritance
d. Multiple inheri tance
Description:
Inheritance in Java is a mechanism in which one objec t acquires all the
properties and behaviors
of the parent object. It is an important part of OOPs (object-oriented
programming systems).
The idea behind inheritance in Java is that you can create new classe
s that are built upon
existing classes. When you inherit from an existi ng class, you can reuse
methods and fields of
the parent class, and you can add new methods and fields. Inheritance
relationship, also known as the parent-child relatio nship. represents the IS-A
Term s used in Inheritance
• Class: A class is a group of objec ts which have common prope
rties. It is a template
or blueprint from which objec ts are created.
• Sub Class/Child Class: A Subclass is a class that inherits the other
class. ft is also
called a derived class, extended class, or child class.
• Supe r Class/Parent Class: Superclass is the class from where
a subclass inherits
the features. It is also called a base class or a parent class.
• Reusabllity: As the name specifies, reusability is a mech anism
that facilitates you
to reuse the fields and methods of the existing class when you create
a new class.
You can use the same fields and methods alread y defined in the previo
us class.
Single Inheritance
i, ;::r • _. ,; · inh eri tan cel;
.- , 'J -.I -i
~) _.. • r._ ,. 1 -,
,_ .... ,..,. , r..) , -,' f' t_' · ( ..· {
int leng th,b read th;
~o pub lit Area {i nt x, int y) {
6 this .len gth= x;
, th is .bre adth =y;
~ }
:i• pub lic int calc _ar· ea {) {
!0 ,·etu rn [(}th is . leng th* t h 1 c, . brea dth) ~
11
...' }
l3
Multilevel Inheritance
[Link]
multi level inher itanc e;
Motorbike.,lava
multi leveli nheri tance ;
\ 1 •,1 11 i,,1l.1 r•, () {
. displa yFeat ures( ); J, ;,_. ..,'. ll •,.
. out . printl n( " ;,,ci,i,·it teal ui·i.: \-.,y ~.-,)t•:,t·L i k.-~.
Electric [Link]
o r ,,., •.. ,r 1 ... ,.
multil evelin herita nce;
.:-.;~f:-n. i·. , 1.·,t,,1 :~•ikf~ l{l
d: . 1 .1·, 1 t:-1t_u,~e'J () {
.displa yFeatu res(); 1 .' O' •,\'-.ilv f,,,,, __ .. ,... ,_,f ... , .
. out .pr·int ln ( "1,,,,: f ,·,1\.ur·,_- b>,• ... t . i'.
mul t ilev~li. nheri lance;
., , ( ,1,.,· {l args ) {
myEbike = , ., flectric Btke();
myEbik e.d1spl ayleatu res();
Iii Pro bl em s <D Ja\'adoc a Declaration • Console -,
Vo lum e=6 000
32
1nht•1' 1 ldf\1.1.'l;
height;
( . ' :-. , ,1 Y, .: ) {
,, ( , , y) ;
.height=.:;
: ' • f: () {\
,1 .calc_area()• 1ii1· .. height;
inheritancel;
0 ·, t.! ,,·,.,,~(. vPid 11;a111 ( ~t ring [) args) {
obj= neu volume( 10, 2O , 30);
-, . , '.:, 1, . out. println( "Volume=" +obj. calc_ volume ());
y ,. • • I a . ,.. , ., • , or,,,ote
, ;/ r,, · r , > t : ••
~eature s : Pedals, Manual power
Added feature by Motorbike: Engine
New feature by ElectricBike: Electric mot
liien, rchica l Inher itance
D ,\JAvA D B1ha I Ciava D Oj,va I Ma1nOau1ava
l. [\cj( I( rll::/· hier arch ical l;
)
3 publ i c cl ass A {
4• pub lic void methodA ()
5 {
6 Systcm .out .pri ntln ( "method of Class A" );
D A1~•~ D_61a~_:_ a Ciava D DJaVl! D MninClassJava
~ package hier arch ical l;
2
~ publ ic clas<, R exte nds A
:, {
;o publ ic vo i d methodB( )
6 {
7 System. out. prin tln( "method of Cl ass B' ) ;
D ,•qava a Btava D [Link] < ·D O java D IAatnClass.1ava
1 ~ack aee hier arch ical l;
L
~ publ ic clas s c ext ends A
.': [1
publ ic void metho dC( )
G {
7
syst em. out. prin tln( "meth od of Clas s C' );
3 }
':I }\
35
VIVA QUESTIONS (PRELAB & POSTLAB):
can you instantiate an object for a class with a subclass overriding its base class's
1
~nstrUctors? . .
c can 8 base-type access pr_operty m ,ts sub-type~?.
2· wnat is method overloading and method o~erndmg? .
!: What is the difference be~een Polymorphism and Inheritance?
s. What is meant by reusab1hty?
39
0 D ' , .· D , ,.,. , D l) [Link],1 a Ma ,nO,,:,, [Link]
h ie ra rc h ic al l;
t-.~·n .o u t. p ri n tl n ( "m et ho
d of C la ss D' );
n \ ., . , • r, •.,..., D t.\.,, ne1,,.., 11v~,
hi e ra rc hi ca ll · - - 1
- , -
·., :ti < •:Q i d mr lin ( SI r•j n t!
(] ar gs ) {
o bj 1 = 1 1, ...~ B( );
o bj 2 = f H "'.•J C( );
ob j 3 = ,. (''.• / D( );
/\ . ( l , I">, -.,-~ ~ , c1 n ~c ce s~ th e me tho d
o bj [Link] th od A( ); of cl ~s ~ A
ob j 2. me th od A( );
ob j3 . me th od A( );
a Prut•\""'~
• • 1~- ·•··•· ·: · _ -• ,..,---
--,,.-
e Jd'ladO< I. Ott lar1111on : • Con sole :•
., , ••• ~ ,.1,,,n,.~ID~:. 1.l1 ,Jd.._a A
\1ca t,onl C\\J se-r :.\n.,<h1\ vl·,t .'('·~
•.p: l .111 , ·1 1,.__-
m et ho d of C la ss A
m et ho d of C la ss A
m et ho d of C la ss A
Int erface- I
D pu,.~., ~ ' D i_:t.!1•~ ~ D 'fn:Cl,11.),.j,wa
1 packa ge multi ple;
2
3 public interf ace Pll {
4• defau lt void show ()
5 !{]
6
7 I I . Pri ,{t · si:at~m ent' if ·method is cal led
8 I I from "interf a ce 1 · '- ,. - -
9 System .out.p rintln (" Defau lt Pll" );
, 10 ·.-
. '' '}I ;,.•,.•-~-
... ,, ,,.·
'_ .. T.' 1.i.~
.TA••~~
7•.•···. -
' ' • .,-, . , llol:Z<,1(',i;t,••·. _. ., . . ·.
,, ,,.. _-
11 }
12
i
lnt c rfa cc-2
D P\[Link]\'a x D •Tf.'"Oo~ov~
1 pack age multi ple;
13 >I
Multiple Inheritance
' .. - ., lnterface•l ..
~~,-,--;---
Oru -1~va
- -
· D [Link]-:a D •T~a~ava
. .- . - . . ---
l package multiple;
2
3 public interface Pll {
4• default void show()
5 ffi
5
7 // Print statement if method is called
8 // from .interf~ce 1
9 [Link]("Default
. .. . ... . .
Pil");
, ~ _'.. '.'. '; .
10 . . ., ·: '~- . ·_ ~~-:~- ..
11 }
12
1av Iva __ ._, __ _ :_;_,_:, --:: _ __,_ ... ·· ___ __
l pa ckage multiple;
2
3 public i nterface PI2 · l{l .
4 ~-.
5 // Default method •
6° default void s how() _.
7 {
9 II Print statement if method is called
10 // from interface 2 _
11 [Link] .printlnC'Default PI2");
12 }