0% found this document useful (0 votes)
2 views30 pages

Java Lab Manual

Uploaded by

ssumanjnv11
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)
2 views30 pages

Java Lab Manual

Uploaded by

ssumanjnv11
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/ 30

ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY BHOPAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Jan- June 2025


B.Tech.

SEMESTER – IV

LAB MANUAL
“ PROGRAMMING PRACTICES”

(CS-406)

Submitted By:-

Name :-

Enrollment no:-

Submitted To:-
ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY BHOPAL
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
B.Tech Sem-IV CS-406
Objective:

T o in tro d u c e a n d u n d e rs ta n d s tu d en ts to p ro g ra m m in g c o n c e p ts a n d tec hn iq u es u s in g
th e J a v a la n g u a g e a n d p ro g ra m m in g e n viro n m en t, c la s s , o b je c ts , a ls o lea rn a b o u t
lifetim e , s c o p e a n d th e in itia liza tio n m ec h a n is m o f v a ria b le s a n d im p ro ve th e a b ility
g e n era l p ro b lem s o lv in g a b ilities in p ro g ra m m in g . B e a b le to u s e th e J a va S D K
en v iro n m e n t to c re a te , d e b u g a nd ru n s im p le J a va p ro g ra m .

SYLLABUS

Unit I: Basic Java Features:

B a s ic J a va F ea tu res - C + + V s J A V A , J A V A virtu al m a c h in e, C o n s ta n t & V a ria b le s , D a ta


T yp e s , C la s s , M eth o d s , O b jec ts , S trin g s a n d A rra ys , T yp e C a s tin g , O p era to rs , P re c e d e n c e
rela tio n s , C o n tro l S ta tem e n ts , E xc ep tio n H a n d lin g , F ile and S tre a m s , V is ib ility,
C o n s tru c to rs , O p e ra to r and M eth o d s O ve rlo a d in g , S ta tic M e m b ers , In h e rita n c e :
P o lym o rp h is m , A b s tra c t m e th o d s a n d C la s s e s

Unit II: Java Collective Frame Work

J a v a C o llec tiv e F ra m e W o rk - D a ta S tru c tu res : In tro d u c tio n , T yp e -W ra p p er C la s s es fo r


P rim itiv e Ty p es , D yn am ic M e m o ry A llo c a tio n , L in k ed Lis t, S ta c k , Q u eu e s , T ree s , G en e ric s :
In tro d u c tio n , O ve rlo a d in g G e n eric M e th o d s , G e n eric C la s s es , C o lle c tio n s : In te rf a c e
C o lle c tio n a n d C la s s C o lle c tio n s , L is ts , A rra y Lis t an d Itera to r, Lin k ed Lis t, V ec to r.
C o lle c tio n s A lg o rith m s : A lg o rith m s o rts , A lg o rith m s h u ffle , A lg o rith m s rev ers e, fill, c o p y,
m a x a n d m in A lg o rith m b in a ry S ea rc h , A lg o rith m s a d d A ll, Sta c k C la s s o f P a c k a g e ja va .
U til, C la s s P rio rity Q u e u e a n d In te rf a c e Q u e u e, M ap s , P ro p e rties C la s s , U n m o d ifia b le
C o lle c tio n s .

Unit III: Advance Java Features

A d v a n c e J a va F e a tu re s - M u ltith re a d in g : T h rea d S ta te s , P rio ritie s a n d T h rea d S c h e d u lin g ,


Life C yc le o f a T h re a d , T h rea d S yn c h ro n iz a tio n , C rea tin g an d E xe c u tin g T h re a d s ,
M u ltith re a d in g w ith G U I, M o n ito rs a n d M o n ito r L o c k s . N e tw o rk in g : M a n ip u la tin g U R Ls ,
R e a d in g a file o n a W eb S e rve r, S o c k e t p ro g ra m m in g , S e c u rity a n d th e N etw o rk , R M I,
N etw o rk in g , A c c e s s in g D a ta b a s e s w ith J D B C : R ela tio n a l D a ta b a s e, S Q L , M yS Q L, O rac le
Unit IV: Advance Java Technologies

A d v a n c e J a va T e c h n o lo g ies - S e rv lets : O v erview a n d A rc h itec tu re , S e ttin g U p th e A p a c h e


T o m c a t S erv er, H a n d lin g H T T P g et R e q u e s ts , D e p lo yin g a w eb A p p lic a tio n , M u ltitie r
A p p lic a tio n s , U s in g J D B C fro m a S ervle t, J a v a S erv er P a g es (J S P) : O v ervie w , F irs t J S P
E x a m p le, Im p lic it O b jec ts , S c rip tin g , S ta n d a rd A c tio n s , D ire c tive s , M u ltim e d ia : A p p le ts a n d
A p p lic a tio n : Lo a d in g , D is p la yin g a n d Sc a lin g Im a g e s , A n im a tin g a S e rie s o f Im a g es ,
Lo a d in g a n d p la yin g A u d io c lip s

Unit V: Advance Web/Internet Programming (Overview

A d v a n c e W e b / In tern et P ro g ra m m in g (O ve rv iew ): J 2 M E , J 2 E E , E J B , X M L .
ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY BHOPAL
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
B.Tech Sem-IV

LIST OF EXPERIMENTS

Experiment Date of Signature &


Aim
No. Submission Remarks

1.
Installation of J2SDK
2. Write a program to show Scope of Variables.

3. Write a program to show Concept of CLASS in


JAVA.

4. Write a program to show Type Casting in JAVA

5. Write a program to show How Exception Handling


is in JAVA

6. Write a Program to show Inheritance

7. Write a program to show Polymorphism.

Write a program to show Access Specifier (Public,


8. Private, Protected) in JAVA
.
9. Write a program to Add a Class to a Package.

10. Write a program to hide a Class


ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY BHOPAL
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
B.Tech Sem-IV

LABORATORY PLAN

Department CSE Session Jan-2025

Name of Faculty Semester IV

Subject Programming Practices Subject Code CS-406

Planned Date
Exp. CO Actual Date of HOD
Experiment of Remarks
No. Completion Verification
Completion

1 CO1
Installation of J2SDK

Write a program to show Scope of


2 Variables. CO1

Write a program to show Concept


3 of CLASS in JAVA. CO2

Write a program to show Type


4 Casting in JAVA CO2

Write a program to show How


5 Exception Handling is in JAVA CO3

Write a Program to show


6 Inheritance CO3

Write a program to show


7 Polymorphism. CO3

Write a program to show Access


Specifier (Public, Private,
8 Protected) in JAVA CO3

.
Write a program to Add a Class to
9 a Package. CO3

Write a program to hide a Class


10 CO3

EXPERIMENT -1

JAVA:

J a v a is a m u lti-p la tfo rm , o b jec t-o rien ted , a n d n etw o rk -c en tric la n g u a g e th a t c a n b e u s ed a s


a p la tfo rm in its elf. It is a f a s t, s ec u re , re lia b le p ro g ra m m in g la n g u a g e fo r c o d in g ev eryth in g
f ro m m o b ile a p p s a n d e n te rp ris e s o ftw are to b ig d a ta a p p lic a tio n s a n d s erv er-s id e
te c h n o lo g ies .

INSTALLATION OF J2SDK:

In tro d uc tio n :

The Java Development Kit (JDK) is software used for Java programming, along with the Java
Virtual Machine (JVM) and the Java Runtime Environment (JRE). The JDK includes the compiler
and class libraries, allowing developers to create Java programs executable by the JVM and JRE.

Download Java for Windows 10

D o w n lo a d th e la tes t J a v a D ev elo p m e n t K it in s ta llatio n file fo r W in d o w s 1 0 to h a ve th e


la tes t fea tu res a n d b u g fixe s .

1. Using your preferred web browser, navigate to the Oracle Java Downloads page.
2 . On the page, click the x64 Installer download link under
th e Windows c a te g o ry. A t th e tim e o f w ritin g th is a rtic le, J a va v ers io n 1 7 is the la tes t
lo n g - term s u p p o rt J a va ve rs io n .

W ait f o r th e d o w n lo a d to c o m p lete.

Install Java on Windows 10

A fter d o w n lo a d in g th e in s ta lla tio n file , p ro c e ed w ith in s ta llin g Ja v a o n yo u r W in d o w s

s ys te m . F o llo w th e s tep s b elo w :

Step 1: Run the Downloaded File

Double-click the downloaded file to start the installation.


Step 2: Configure the Installation Wizard

A fter ru n n in g th e in s ta lla tio n file, th e in s ta lla tio n w iz ard w elc o m e s c ree n a p p e ars .

1. Click Next to proceed to the next step.

2. Choose the destination folder for the Java installation files or stick to the
default path. Click Next to proceed.
EXPERIMENT -2

VARIABLE AND SCOPE:

Scope of a variable is the part of the program where the variable is accessible. Like
C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e. Scope of a
variable can determined at compile time and independent of function call stack.

Java programs are organized in the form of classes. Every class is part of some
package. Java scope rules can be covered under following categories.

Member Variables (Class Level Scope):

These variables must be declared inside class (outside any function). They can be
directly accessed anywhere in class. Let’s take a look at an example:

p u b lic c la s s Tes t

/ / A ll v a ria b le s d ef in e d d irec tly in s id e a c las s

/ / a re m e m b e r

v aria b le s in t a ;

p riva te S trin g b ;

v o id m eth o d 1 () { }

in t m eth o d 2 () { }

cha r c;

● We can declare class variables anywhere in class, but outside methods.


p ub lic Y es Y es Y e s

p ro tec ted Y es Y es N o

D efa u lt (n o
m o d ifier) Y e s No No

p riva te No NoNo

V a ria b les d ec la red in s id e a m e th o d h a ve m e th o d lev el s c o p e a n d c a n ’ t b e a c c es s e d


o u ts id e th e m e th o d .

p u b lic c la s s Tes t

v o id m etho d 1 ()

// L o c a l v a ria b le (M e th o d lev el

s c o p e) in t x ;

Note : L o c a l v aria b le s d o n ’ t e xis t a fte r m e th o d ’ s e xe c u tio n is o v er.


H e re ’ s a n o th er ex am p le o f m eth o d s c o p e, e xc ep t th is tim e th e v a ria b le g o t p as s e d in
a s a p a ra m eter to th e m eth o d :

c la s s T est

p riva te in t x;

p u b lic v o id s e tX (in t x )

th is .x = x;

T h e ab o v e c o d e u s es th is k e yw o rd to d iffe re n tia te b e tw e en th e lo c a l a n d c la s s
v a ria b le s . A s a n ex erc is e , p red ic t th e o u tp u t o f fo llo w in g J a va p ro g ra m .

● Java
EXPERIMENT- 3

CLASS IN JAVA:

1. Class is a set of object which shares common characteristics/ behavior and


common properties/ attributes.
2. Class is not a real-world entity. It is just a template or blueprint or prototype from
which objects are created.
3 . Class does not occupy memory.
4. Class is a group of variables of different data types and a group of
methods. A class in java can contain:
● data member
● method
● constructor
● nested class and
● interface
S yn ta x to d ec la re a c la s s :

a c c e s s _m o d ifier

c la s s < c la s s _n a m e>

d a ta

m em b e r;

m e th o d ;

c o ns tru c to r;

n es te d

c la s s ;

interfac e;
}

E xa m p le:

● Animal
● Student
● Bird
● Vehicle
● Company
classStudent {

intid ; / / d a ta m em b e r (a ls o in s ta n c e va ria b le )

S trin g n a m e; // d a ta m e m b er (a ls o in s ta n c e v aria b le)

publicstaticvoidmain(String args[])

S tu d e n t s 1 = newS tu d en t(); // c re a tin g a n o b jec t o f

/ / S tu d e n t

S ys tem .o u t.p rintln(s 1 .id );

S ys tem .o u t.p rintln(s 1 .n a m e );

}
A c la s s is a u s er-d efin ed b lu e p rin t o r p ro to typ e fro m w h ic h o b je c ts a re c rea ted . It
re p res e n ts th e s et o f p ro p e rties o r m eth o d s th a t a re c o m m o n to a ll o b jec ts o f o n e typ e .
In g en era l, c la s s d e c la ra tio n s c a n in c lu d e th es e c o m p o n en ts , in o rd e r:

1. Modifiers: A class can be public or has default access (Refer this for details).

2. Class keyword: class keyword is used to create a class.

3. Class name: The name should begin with an initial letter (capitalized by convention).

4. Superclass(if any): The name of the class’s parent (superclass), if any,


preceded by the keyword extends. A class can only extend (subclass) one parent.

5. Interfaces(if any): A comma-separated list of interfaces implemented by the class,


if any, preceded by the keyword implements. A class can implement more than one
interface.

6. Body: The class body is surrounded by braces, { }.

C o n s truc to rs a re u s ed fo r in itia liz in g n e w o b jec ts . F ield s a re v a ria b les th a t p ro vid e th e


s ta te o f th e c la s s a n d its o b je c ts , a n d m eth o d s a re u s e d to im p lem en t th e b eh a v io r o f
th e c las s a n d its o b jec ts .
T h e re a re v a rio u s typ e s o f c la s s e s th a t a re u s ed in rea l-tim e ap p lic a tio n s s u c h a s
EXPERIMENT – 4

JAVA TYPE CASTING:

In J a v a , typ e c a s tin g is a m e th o d o r p ro c es s th a t c o n v erts a d a ta typ e in to a n o th er


d a ta typ e in b o th w a ys m a n u a lly a n d a u to m a tic a lly. T h e a u to m a tic c o n ve rs io n is d o n e
b y th e c o m p ile r a n d m a n u a l c o n ve rs io n p erfo rm e d b y th e p ro g ra m m er. In th is s e c tio n ,
w e w ill d is c u s s typ e c a s tin g a n d its typ e s w ith p ro p e r e xa m p le s .

Type casting

C o n v ert a v a lu e f ro m o n e d a ta typ e to a no th er d a ta typ e is k n o w n a s type casting.

Types of Type Casting

T h e re a re tw o typ es o f typ e c a s tin g :

o Widening Type Casting


o Narrowing Type Casting
o The target type must be larger than the source type.
1. byte -> short -> char -> int -> long -> float -> double

F o r ex a m p le , th e c o n ve rs io n b e tw e en n u m eric d a ta typ e to c h a r o r B o o lea n is n o t d o n e


a u to m a tic a lly. A ls o , th e c h a r a n d B o o le a n d a ta typ es a re n o t c o m p a tib le w ith e a c h o th e r.
L et's s e e a n ex a m p le.

WideningTypeCastingExample.java

1. public class WideningTypeCastingExample

2. {
3. public static void main(String[] args)

4. {
5. int x = 7;

6. //automatically converts the integer type into long type


7. long y = x;

8. //automatically converts the long type into float type


9. float z = y;

1 0. System.out.println("Before conversion, int value "+x);


1 1. System.out.println("After conversion, long value "+y);
1 2. System.out.println("After conversion, float value "+z);
1 3. }
1 4. }

Output

B e fo re c o n v ers io n ,th e v a lu e is : 7

A fter c o n v ers io n , th e lo n g va lu e is :

7
A fter c o n v ers io n , the flo a t v a lu e is : 7 .0

Intheaboveexample, wehavetakenavariablexandconverteditintoalongtype.Afterthat, thelong


typeisconvertedintothefloat type.
EXPERIMENT – 5

Exception Handling is in JAVA:

T h e E xc ep tio n H a n d lin g in J a v a is o n e o f th e p o w e rfu l m e c h a n is m to h a nd le th e


ru n tim e erro rs s o th a t th e n o rm a l flo w o f the a p p lic a tio n c a n b e m a in ta in ed .

In th is tu to ria l, w e w ill le a rn a b o u t J a v a e xc ep tio n s , it's typ es , a n d th e d iffere n c e


b etw ee n c h ec k ed an d u n c h ec k ed ex c e p tio n s .

Hierarchy of Java Exception classes

T h e ja va .la n g .T h ro w a b le c la s s is th e ro o t c la s s o f J a va E x c e p tio n h iera rc h y in h e rited b y


tw o s u b c la s s es : E x c e p tio n a n d E rro r. T h e h ie ra rc h y o f J a va E x c e p tio n c la s s e s is g iv en
b elo w :
Types of Java Exceptions:

T h e re a re m a in ly tw o typ es o f e xc ep tio n s : c h ec k ed a n d u n c h e c k e d . A n e rro r is


c o n s id e red a s th e u n c h e c k e d e xc ep tio n . H o w e ve r, a c c o rd in g to O ra c le, the re a re th ree
typ es o f e xc ep tio n s n a m ely:

1. Checked Exception
2. Unchecked Exception
3. Error
J a v a E xc ep tio n H an d lin g E xa m p le

L et's s e e a n e xa m p le o f J a v a E xc ep tio n H a n d lin g in w h ic h w e a re u s in g a try-c a tc h


s ta tem en t to h a n d le th e e xc ep tio n .

JavaExceptionExample.java

public class JavaExceptionExample{


public static void main(String args[]){ try{

/ / c o d e th a t m a y ra is e e xc ep tio n

int data=100/0;

} catch (A rith m etic E xc ep tio n e){Sys tem .o ut.p rin tln (e);}

/ / re s t c o d e o f th e p ro g ra m S ys tem .o ut.

p rin tln ("re s t o f th e c o d e. ..");

Output:
E xc ep tio n in th rea d m a in ja va . la n g .A rith m e tic E xc ep tio n :/ b y ze ro res t o f th e c o d e . . .

EXPERIMENT – 6

INHERITANCE:

J a v a , In h e rita n c e is a n im p o rta n t p illa r o f O O P( O b jec t-O rie n te d P ro g ra m m in g ). It is th e


m e c h a n is m in Ja v a b y w h ic h o n e c la s s is a llo w e d to in h erit th e fe atu res (f ie ld s a n d
m e th o d s ) o f a n o th e r c la s s . In J a v a , In h e rita n c e m ea n s c re a tin g n e w c la s s e s b a s e d o n
e xis tin g o n e s . A c la s s th a t inh e rits fro m a n o the r c la s s c a n re u s e th e m e th o d s a n d
f ie ld s o f th a t c la s s . In a d d itio n , yo u c an a d d n ew field s a n d m eth o d s to yo u r c u rre n t
c la s s a s w ell.
In p ra c tic e, in h e rita n c e , a n d p o lym o rp h is m a re u s ed to g eth er in J a v a to a c h iev e fa s t
p erfo rm a n c e a nd re a d a b ility o f c o d e .

Java Inheritance Types


B e lo w a re th e d iffe re n t typ e s o f in h e rita n c e w h ic h a re s u p p o rted b y Ja va .

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance

1. Single Inheritance
In s in g le in h e rita n c e , s u b c la s s e s in h e rit th e fea tu re s o f o n e s u p e rc la s s . In th e im a g e
b elo w , c la s s A s e rve s a s a b a s e c la s s f o r th e d erive d c la s s B .

2. Multilevel Inheritance
In M u ltile ve l In h erita n c e, a d erive d c las s w ill b e in h e ritin g a b a s e c la s s , a n d a s w ell a s
th e d erive d c la s s a ls o a c ts a s th e b a s e c la s s fo r o th e r c la s s e s . In th e b elo w im a g e,
c la s s A s e rv es a s a b a s e c la s s fo r th e d e riv ed c la s s B , w h ic h in tu rn s erve s a s a
b a s e c la s s fo r th e d erive d c la s s C . In J a v a , a c la s s c a n n o t d irec tly a c c es s th e
g ran d p a ren t’ s m e m b e rs .
3.

H iera rc h ic a l In he rita n c e
In H iera rc h ic a l In h erita n c e, o n e c la s s s erve s a s a s up erc la s s (b as e c la s s ) fo r m o re th a n
o n e s u b c la s s . In th e b e lo w im a g e, c la s s A s e rve s a s a b a s e c la s s fo r th e d eriv ed
c la s s es B , C , a n d D .

4. Multiple Inheritance (Through Interfaces)


In M u ltip le in h erita n c es , o n e c la s s c a n h a ve m o re th a n o n e s u p erc la s s a n d in h erit
f ea tu re s fro m a ll p a re n t c la s s e s . P lea s e n o te th a t J av a d o e s not s u p p o rt m u ltip le
in h e rita n c e s w ith c la s s es . In Ja v a , w e c a n a c h ie ve m u ltip le in h erita n c es o n ly th ro u g h
In te rf a c e s . In th e im a g e b elo w , C la s s C is d e riv ed f ro m in te rfa c e s A a n d B .
5. Hybrid Inheritance(Through Interfaces)
It is a m ix o f tw o o r m o re o f th e a b o ve typ es o f in h e rita n c e . S in c e J a va d o e s n’ t s u p p o rt
m u ltip le in h erita n c es w ith c la s s e s , h yb rid in h e rita n c e is a ls o n o t p o s s ib le w ith
c la s s es . In J a v a, we c an a c h ie ve h yb rid in h erita n c e o n ly
th ro u g h Interfac es .
EXPERIMENT – 7

POLYMORPHISM:

T h e w o rd p o lym o rp h is m m e a n s h a v in g m a n y fo rm s . In s im p le w o rd s , w e c a n d efin e
p o lym o rp h is m a s th e a b ility o f a m es s a g e to b e d is p la yed in m o re th a n o n e fo rm .

Real-life Illustration: Polymorphism

A p ers o n a t th e s a m e tim e c a n h a ve d iffe re n t c h a ra c te ris tic s . L ik e a m a n a t th e s a m e tim e


is a fa th e r, a h u s b a n d , a n em p lo yee . S o th e s a m e p ers o n p o s s e s s es d iffe re n t b eh a v io r in
d iffere n t s itu a tio n s . T h is is c a lle d p o lym o rp h is m .
P o ly m o rp h is m is c o n s id e red o n e o f th e im p o rta n t fe a tu re s o f O b jec t-O rie n te d
P ro g ram m ing . P o lym o rp h is m a llo w s u s to p erfo rm a s in g le a c tio n in d iff eren t w a ys . In
o the r w o rd s , p o lym o rp h is m a llo w s yo u to d efin e o n e in te rfa c e a n d ha v e m u ltip le
im p lem e n ta tio n s . T h e w o rd “ p o ly” m e a n s m a n y a n d “ m o rp h s ” m ea n s fo rm s , S o
it m e a n s m a n y fo rm s .

Types of polymorphism
In J a v a p o lym o rp h is m is m a in ly d iv id ed in to tw o typ es :

● Compile-time Polymorphism
● Runtime Polymorphism

Type 1: Compile-time polymorphism

It is a ls o k no w n a s s ta tic p o lym o rp h is m . T h is typ e o f p o ly m o rp h is m is a c h iev ed b y


f u nc tio n o v erlo a d in g o r o p era to r o v erlo a d in g .
Method Overloading : W h e n th e re a re m u ltip le fu n c tio n s w ith th e s a m e n a m e b u t
d iffere n t p a ra m e te rs th e n th es e fu n c tio n s a re s a id to b e overloaded. F u n c tio n s c a n
b e o ve rlo a d e d b y c h a n g es in th e n u m b e r o f a rg u m en ts o r/ a n d a c h a n g e in th e typ e o f
a rg u m e n ts .

classHelper{

// Methodwith 2 integerparameters

staticintMultiply(inta, intb)

//Returnsproduct ofintegernumbers

returna *b;

// Method2

// Withsame name but with 2doubleparameters

staticdoubleMultiply(doublea, doubleb)

//Returnsproduct of doublenumbers

returna *b;

// Class2

// Mainclass

classGFG {

// Maindrivermethod
publicstaticvoidmain(String[] args)

//Callingmethodby passing

//input asinargumentsSystem.out.println(Helper.Multiply(2, 4));

System.out.println(Helper.Multiply(5.5, 6.3));

Output:
8

3 4 .6 5
EXPERIMENT – 8

Access Specifier (Public, Private, Protected) in


JAVA:

in J a va , A c c es s m o d ifie rs h e lp to re s tric t th e s c o p e o f a c la s s , c o n s tru c to r, va ria b le,


m e th o d , o r d a ta m e m b er. It p ro v id e s s e c u rity, ac c e s s ib ility, e tc to th e u s e r d ep en d in g
u p o n th e a c c es s m o d ifie r u s e d w ith th e elem en t. L et u s lea rn a b o u t J a va A c c e s s
M o d ifiers , the ir typ e s , a n d th e u s es o f a c c e s s m o d ifiers in th is a rtic le.

Types of Access Modifiers in Java:

T h e re a re fo u r typ e s o f a c c e s s m o d if ie rs a v a ila b le in J av a:

1. Default – No keyword required


2 . Private
3 . Protected
4 . Public

1. Default Access Modifier


W he n n o a c c es s m o d ifie r is s p e c ified f o r a c la s s , m e th o d , o r d a ta m e m b e r – It is s a id
to b e h a vin g th e default a c c e s s m o d ifier b y d e fa u lt. T h e d a ta m em b e rs , c la s s es , o r
m e th o d s th a t a re n o t d ec la re d u s in g a n y a c c es s m o d ifiers i.e. h a v in g d e fa u lt a c c es s
m o d ifiers are
accessible only within the same package.

Program 1:
/ / J a va p ro g ra m to illu s tra te d e fa u lt m o d ifier
packagep1;
/ / C la s s G ee k is h a vin g D e fa u lt a c c es s m o d ifier
classGeek

voiddisplay()

S ys te m .o u t.p rin tln ( "H ello W o rld !");


}

Program 2:
/ / J a va p ro g ram to illu s tra te erro r w hile

/ / u s in g c la s s fro m d iffe ren t p a c k a g e w ith


/ / d e fa u lt m o d ifier
packagep2;
Expirement-9

Class:

A c la s s in J a va is a lo g ic a l te m p la te to c re a te o b je c ts th a t s h a re c o m m o n p ro p e rties a n d
m eth o d s . H en c e , a ll o b je c ts in a g iv en c la s s w ill h a ve th e s a m e m eth o d s o r p ro p e rties .

Class to a Package:

T h e p a c k a g e s a re u s ed fo r c a teg o riza tio n o f th e s a m e typ e o f c la s s e s a n d in te rf a c e in a


s in g le u n it. T h e re is n o c o re o r in -b u ilt c la s s e s th a t b elo ng to u nn a m e d d e fa u lt p a c k a g e .
T o u s e a n y c la s s e s o r in terfa c e in o th e r c la s s , w e n e ed to u s e it w ith th e ir fu lly q u a lifie d
typ e na m e . But s o m e tim e, w e ’ ve th e n e ed to u s e a ll o r n o t a ll th e c la s s es o r inte rfa c e o f
a p a c k a g e th en it’ s a ted io u s jo b to u s e in s u c h a w a y d is c u s s e d . J a v a s u p p o rts
s ta te m en t to b rin g e n tire p a c k a g e , o r c e rta in c la s s es in to vis ib ility. It p ro vid es flex ib ility to
th e p ro g ra m m er to s a ve a lo t o f tim e ju s t b y im p o rtin g th e c la s s e s in h is / h e r p ro g ra m ,
in s tea d o f re w ritin g th e m .

In a J a v a s o u rc e file , import s ta tem en ts o c c ur im m ed ia tely fo llo w in g th e package s ta te m e n t


(if it ex is ts ) a n d m u s t b e b e fo re o f a n y c la s s d ef in itio n s . T h is is the g e n era l fo rm is a s fo llo w s :

Built-in Packages:

T h e J a va A P I is a lib ra ry o f p re w ritte n c la s s e s , th a t a re free to u s e , in c lu d e d in th e J a v a


D e ve lo p m e n t E n viro n m en t.

T h e lib ra ry c o n ta in s c o m p o n e n ts fo r m a n a g in g in p u t, d a ta b a s e p ro g ra m m in g , a n d m u c h
m u c h m o re. T h e c o m p le te lis t c a n b e fo u n d a t O ra c les w eb s ite:

T h e lib ra ry is d ivid ed in to packages a n d classes. M ea n in g yo u c a n eith er im p o rt a


s in g le c la s s (a lo n g w ith its m e th o d s a n d a ttrib u tes ), o r a w h o le p a c k a g e th a t c o n ta in a ll
th e c la s s e s th a t b elo n g to th e s p ec ified p a c k a g e.

Syntax:
program:

package p1;

class C1()

public void m1()

S ys tem .o u t.p rin tln ("m 1 o f C 1 ");

public static void main(string args[])

C 1 o b j = new C 1 ();

o b j.m 1 ();

}
Experiment- 10

Hiding Class
H id d en c la s s es a re c la s s es th a t c a n n o t b e u s ed d ire c tly b y th e b ytec o d e o r o th er
c la s s es . E v en th o u g h it's m en tio n e d a s a c la s s , it s h o u ld b e u n d e rs to o d to m ea n eith er a
h id d en c la s s o r in te rfa c e . It c a n als o b e d efin ed a s a m em b e r o f th e a c c e s s c o n tro l n es t
a n d c a n b e u n lo a d ed in d ep en d en tly o f o th er c la s s es

How to Write Hidden Classes


T h e h id d e n c la s s is c re a te d b y in v o k in g

L o o k u p ::d e fin eH id d en C las s

. It c a u s es the J V M to d e riv e a h id d en c la s s fro m th e s u p p lied b yte s , lin k s th e h id d e n c la s s ,


a n d re tu rn s a L o o k u p o b je c t th a t p ro v id es refle c tive a c c e s s to th e h id d en c la s s .

The following are 4 steps for creating and using


hidden classes.
Create Lookup object. Get a lookup object, which will be used to create hidden
class in the next steps.

M e th o d H a n d les . Lo o k u p lo o k u p = M eth o d H an d les .lo o k u p ();

Create class bytes using ASM. We are using the byte code manipulation library
ASM. We create ClassWriter object using helper class GenerateClass. If you look at the
details in GenerateClass, this class implements interface Test, which we will use in further
Use hidden class. In this step, we are using reflection to access the hidden class.
First, create the constructor, then create an object using this, typecast this object to
interface Test and call method test. This method ignores the argument passed and prints
"Hello test" to the console.

C o n s truc to r< ? > c o n s tru c to r = c . g e tC o n s tru c to r(n u ll);

O b je c t o b jec t = c o n s tru c to r.n ew In s ta n c e (n u ll) ;

T e s t tes t = (T es t) o b jec t;

te s t.te s t(n e w

S trin g []{""});

How to Hide Class in a Package


W h e n w e im p o rt a p a c k a g e u s in g a s tric (*),a ll p u b lic c la s s es a re im p o rte d . h o w ev er ,w e

m a y p reffe r to “ n o t im p o rt” c erta in c la s s es .i.e ,w e m a y lik e to h id e th es e c la s s es fro m

a c c e s s in g f ro m o u ts id e o f th e p a c k a g e .s u c h c la s s e s s h o u ld b e d ec la re d ” n o t p u b lic ” .

EX:

p a cka ge p1 ;

p u b lic c la s s X

B od y of X
}

c la s s Y
{

B od y of Y
}

You might also like