0 ratings0% found this document useful (0 votes) 20 views17 pagesJava Assignment-1
Java important questions for semester exams for 3rd semester
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
r Thursday
ABI-aH ASSIGNMENT - 03
=— == =>
v Define inheritance and éxplato the digevert te 4
iphentanee yaith relevant examples «
7% aa into
Acquiring the propertiee from one class (parent class)
Gub class (child etass) ts caned Sohenitan ce”
~ Parent class 18 also called super class ov base class and
child class ts called as a Sub-class OF deived class -
‘Type 4 Tnheritanee !
\ Single - level “fin evitanee |
sub class
Wheo a class inherits propatis from another class then iF is
called at Single « level ~inhentanes «
$x! class Animal
L
void eat C)
t
Syetern out : painelo (“eating”) ;
42class Dog extends Anima)
t
void bavk()
c
Sysern out priotto ("barking");
| s
J
Class Test Tohentanee
t
public static void main ( String CIarqs)
t
Dog dz new Dog 0) 5,
d+ baat 095
deat;
y
y.
Output *
barking
eating.
AY Mult level ~ Sheritance ?
Class -C|
|
:
| Wathen there {8 a chain of inh ertance , then it '¢ Known ag
multi-level Tnhevtanee -Example!
Class Animal
t
vod att)
t
System: out * print lo (“eating”);
3
Claes hog extends Animal
t
void barke)
t
System: cut: printto ("barking");
4]
3
class Baby Dog extends Dog
t
vord sleep c
s
System sout print (ucteep”);
x
+
Class Test Inhenitanee
public static wid mai (String args C1)
<
Baby Dog bs new Baby Pog Oe
b: sleepO)s
b- bavk0);
5 y b- eat); 44
aput ut! Sleeping a
barking
eating,
37 Heivarehical = Level-Tnhevitanee:
9 two oF more classes Inherit King\e class then {4 ts Knoven
whet
as Herarchical -Level~ Mheitance -
sxample
class Animal
eC
void wat Cc)
ic
System + out + printlo (“eating”);
3
class Dog extends Anima)
t
void bark ¢)
€
Systern out “print lo (“'baaktng")5
y
s ;
class cat extends Anienal
out: prtatta (95: 4S);
System out: pratty (°K! 94K);
fot Colthy 4
System out printia ‘ita
3
class main class
<
public stare void mai (Sting Ca8qs)
Bb » new @ (1421395
br add HO 50
NN eeebraddikey; *
3
af
gurput' (it}>)3
th
Ki3
(ab t4 y.
8) Super 1 used {p invoke ‘gupe? clase hittodl!
~hheo parent class method and child dass method names are
damt {0 otherusords the method ts overiden -
- class Animal
4
votd eat ©)
t
System - out: griatlo (rath animals can eat");
yord cat)
L
|
|
|
3
class Dog extends Animal
System -out “pantlo (eating bread”) 5
iy
void bak)
4
System: out priate (“basking ") 5
3 51
ee ae ee a ee ee| : 1
Void usorke )
4
Super -catC) 5
Super barker;
3
Class main class
t
public State void mato (Sting args C3)
¢
Dog d+ new Dogt);
d-soak 0);
4
Sutput AM animals can eat
PP hwite a program yohich {mplemanés the usage oy JAVA
method overiamg
Tt te used 0 provide Specific implementation ya method
that je alrady provided by its Super etoss »
R te basteatty used for oun, time polyriophiem «
class Bank
v
int getRate of Aoeerest 0) ¢
xetumn 0;
a 82
—_—eeeeclass SOL extends Bank &
fat get Rateop Tnterest (9 £
etn 8)
¥
s
crass 1CtCl extends Bank ¢
int getRateof Loterest 69 ¢
vetunn F 5
s
J
Class AXIS extends Bank ¢
int get Rateog Taterest 9 £
retum 4)
y
5
tlass Test 9 ¢
public statie void main (String CJ.axg8) ¢
SBI Se new SBI 0);
Jere) | = nevo icteley,
Axis az neva AXIS CO);
Syston out: pret ("S01 Rate of Tateest °F S* get Rate of
Jaterest) j
Systeen- out’ prinetn (eter Rate af Interests” ¢ (gp tle eat
oO);
;
53
ie ee ee eee eeni
Systera out: print (“Axis Rate af Anterest 2" 4 age hate of
' LatorestO) 5
eY,
Output! SB Rate of Tntorest ! 8
lerel Rate of tnterest |
Anis kate &p gaterest Ty,
4 Define abshact classes and Lxplain the use oF abstract method,
and classes sith velevant examples »
~ Abshact Classes! We aan digine a superclass that declares the
ere of a given abshacton, usithout providing a Complete
implementation af every method. ‘fhe gunaral form of a9 abstract
dass typename Cpavameter - ist ) 5
~ using abstact methods and classes!
abstract class Figure €
double dim 4!
double dim 9 +
Figure (double a, double b) t
dim dea,
dim 92,
is,
abshact double area C)5
f 6class Rectangle ortends Figure t
Rectangle (double a, double b) ¢
Super (a,b) ‘
y
double areal)
System -out * printto (“Sherde Area for fastangle.”) 5
rebum diel din 25
¥
4
class Triangle extends Figure t
Frangle (double a , double b) eC
Supyr (arb oh
x
double areal) 3 aH
Systern - out printlo ("Dneide Area for Triangu - )5
rear dim) aim] & ;
¥
A
class Abstract Anat
public static void mato (String CJargs) €
/| Figure = new figure (10) 5
fettong tu o> co Rectang le (9:8);
friangle £= new ‘Triangle (10,8 J;
Figure fignet 5 a
Pe OS eeAgr -r; ;
System - out’ printlo ("Area te "4 gignep a7e0)3
figref et) :
Syste out: prinélo (“Area is” a feguep ard);
¥
x
Butput’ theide area for reciangle area is 45-60
Inside area for Trhangls aua '¢ 40°04.
BY Depi
tfine Tntegace by undustanding the relationship. betuown classes
and jotexawe usith guitable example
os '
Teteyace! In Java, an interjace fe a blue print of a class «
FR has static conctants and abshact methods -
~ The liteaee in Java te a mechaniem to acheive abstraction.
~ Undastand yetatonship between classes and Interfaces!
An jntexface ie defined much like a class -General foun of an interface !
access intaqace name t
type final _— varname 4 = value)
type Avaal _ varname 9 = value}
44ype Pinal — yasname N= value }
ret “Aype method -name 4 (pavametor ~ list ) 5
Yeturn - type method- name 9 (parameter - 184 ) 5
,
- Interface Sayable &
Statice void say Louder (String meq) 4
System put pratlo (msg) 4
¥
3
public class Default Methods implemends Sayable €
public static void main (Shing CJarqs) ¢
Sayable saylouder (“Hetloooo- Boe 5)
x
J.
Output « Helloooo..-..
57a”
tiple foheritane, fn i
6 nite program ughich implements maul
Tava by inteyaee -
ode}
Interface Sayable
4
default void say C)
Say Something 0) 5
¥ :
Pavate void Saysomething ()
4
‘ F Syston out® priatlo (*Hello--- Sm private method”):
Ss
ee class private Tukuface Spleen ts Sayable
i public stave vord main [ Sting CI 429s)
L
Sayable = neva Private Tkerface 095
S sayl);
5
4
Outpur! Hello... T'm private method n,
58