FOR EXAMPLE:
Community fixed last int a = 10;
Secured int d;
int x; [we are not using any accessibility specifier it is handled as standard accessibility modifier]
Personal int e;
NOTE:
1.Private accessibility specifier is also known as local accessibility specifier
2.Default accessibility specifier is also known as system accessibility specifier.
3.protected accessibility specifier is also known as monetary gift accessibility specifier.
4.Public accessibility specifier is also known as worldwide accessibility specifier.
WRITE A JAVA PROGRAM WHICH ILLUSTRATES THE CONCEPT OF ACCESS RULES?
ANS:
//Sbc.java
// javac -d . sbc.java
system sp
public category Sbc
{
private int N_PRI=10;
int N_DEF=20;
protected int N_PRO=30;
public int N_PUB=40;
public Sbc()
{
Program.out.println ("VALUE OF N_PRIVATE = "+N_pri);
Program.out.println ("VALUE OF N_DEFAULT ="+N_DEF);
Program.out.println ("VALUE OF N_PROTECTED ="+N_PRO);
Program.out.println ("VALUE OF N_PUBLIC ="+N_PUB);
}
};
//Sic.java
// javac -d . sicjava
package sp;
public category Sic
{
Sbc so=new Sbc(); //(has-a regards & within only)
public Sic()
{
//System.out.println ("VALUE OF N_PRIVATE = "so.N_PRI);
Program.out.println ("VALUE OF N_DEFAULT = "so.N_DEF);
Program.out.println ("VALUE OF N_PROTECTED = "so.N_PRO);
Program.out.println ("VALUE OF N_PUBLIC = "so.N_PUB);
}
};
//Odc.java
//javac -d Odc.java
package op;
public category Odc expands sp.Sbc // (is relatoin & across)
{
public odc()
{
//syatem.out.println ("VALUE OF N_PRIVATE = "+N_PRI);
//System.out.println ("VALUE OF N_DEFAULT ="+N_DEF);
Program.out.println ("VALUE OF N_PROTECTED ="+N_PRO);
Program.out.println ("VALUE OF N_PUBLIC ="+N_PUB);
}
};
//Oic.java
//javac -d Oic.java
packages op;
public category Oic
{
sp.Sbc so = new sp.Sbc (); // (has-a regards & across)
public Oic()
{
//System.out.println ("VALUE OF N_PRIVATE = "so.N_PRI);
// Program.out.println ("VALUE OF N_DEFAULT = "so.N_DEF);
//System.out.println ("VALUE OF N_PROTECTED = "so.N_PRO);
Program.out.println ("VALUE OF N_PUBLIC = "so.N_PUB);
}
};
//ASDemo.java
// java ASDemo.java
import sp.Sbc;
import sp.Sbc;
import sp.Sic;
class ASDemo
{
public fixed gap primary (string [] args)
{
//import approach
Program.out.println ("WHIT RESPECT TO SAME PACKAGE BASE CLASS");
Sbc so1=new Sbc();
Program.out.println ("WHIT RESPECT TO SAME PACKAGE DERIVED CLASS");
Sbc so2=new Sbc();
Program.out.println ("WHIT RESPECT TO SAME PACKAGE INDEPENDENT CLASS");
Sic so3 = NEW Sic();
//fully certified name approach
Syatem.out.println ("WHIT RESPECT TO SAME PACKAGE DERIVED CLASS");
op.Odc.oo1=new op.Odc();
Program.out.println ("WHIT RESPECT TO SAME PACKAGE INDEPENDENT CLASS");
op.Oic oo2=new op.Oic();
}
};
NAMELESS OBJECT APPROACH:
Sometimes there is no requirement for the JAVA developer to make a product with some name. In such scenario we can use the concept of unidentified object.
FOR EXAMPLE:
//named object approach
Check t1= new Check ();
t1.display ();
To turn the above claims into unidentified object strategy adhere to the following claims.
FOR EXAMPLE:
//nameless object strategy
new Check ().display ();
No comments:
Post a Comment