Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
package & protected access modifier

 
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum
Author Message
Niloy
Beginner User
Beginner User


Joined: 07 Mar 2004
Posts: 32

Post Post subject: package & protected access modifier Reply with quote

Please tell me about protected members of a class.Also distinguish me between package access modifier & protected access modifier with a example
_________________
I am a lover not a fighter.
Thu Mar 25, 04 2:13 am
Back to top
Niloy View user's profile Send private message Yahoo Messenger
Belal
User
User


Joined: 08 Mar 2004
Posts: 84
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Niloy, u r seeming new to java.

anyway, in java there r four types of access modifier( default, public, private and protected). u r asking for protected members to be described only. this means u know others well. we can think protected members in between public and private members. this means the area of protected members are in between this two. public members can b accessed from any other class outside from that class whereas private members can b accessed only in the class where it is defined. But protected members can b access by any member class file of a package. this means any class file under a package will recognise those members. e.g. suppose a package has three class file A,B, and C. if class A has any public member i,j,k; private member o,p,q and protected member x,y,z, then we can say---any methods of class A will recognise all these memebers, B and C class will recognise only public(i,j,k) and protected(x,y,z) members of class A. if we make a new class named it D. then D can only access the public(i,j,k) members of class A not private nor protected members.

i m not clear abt ur second query, hope u clearify that.
thanks
Thu Mar 25, 04 2:33 am
Back to top
Belal View user's profile Send private message Yahoo Messenger MSN Messenger
Niloy
Beginner User
Beginner User


Joined: 07 Mar 2004
Posts: 32

Post Post subject: package access modifier & protected access modifier. Reply with quote

Thanks, Mr.Belal answaring my question. Actually I want to know what is the difference between package access modifier & protected access modifier. If you kindly explain this with a example it would be helpful to me.


Thanks,
Your Niloy
_________________
I am a lover not a fighter.
Sat Mar 27, 04 7:31 am
Back to top
Niloy View user's profile Send private message Yahoo Messenger
Belal
User
User


Joined: 08 Mar 2004
Posts: 84
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Niloy,
i don't anything as package access modifier, so if u know somthing more abt it u can explain the term and then it will b possible for me to help u understand the term.
and u liked to know abt protected access modifier. i think i've told abt that in my early discussion. if u still have problem i think the problem is in understanding package basic. here i've given some code compile them and try to understand them, hope these will clear urself abt package as well as protected and other access modifier.

//First Program
package Bangladesh;

public class Protection
{
String i_am_def="I am default"; //a default variable
private String i_am_pri="I am Private";
protected String i_am_pro="I am Protected";
public String i_am_pub="I am Public";

public Protection()
{
}

public void printClassInfo()
{
System.out.println("Root Information are ---Protection Class: ");
System.out.println("i_am_def= "+i_am_def);
System.out.println("i_am_pri= "+i_am_pri);
System.out.println("i_am_pro= "+i_am_pro);
System.out.println("i_am_pub= "+i_am_pub);
}
}

//Second Program
package Bangladesh;

class Derived extends Protection
{
public Derived()
{
System.out.println("Derived Information are ---Derived Class: ");
System.out.println("i_am_def= "+i_am_def);
//System.out.println("i_am_pri= "+i_am_pri);
System.out.println("i_am_pro= "+i_am_pro);
System.out.println("i_am_pub= "+i_am_pub);
}
}

//Third Program
package Bangladesh;

class UnderBangladeshPackage
{
public UnderBangladeshPackage()
{
Protection p=new Protection();
System.out.println("UnderBangladeshPackage Information are ---");
System.out.println("i_am_def= "+p.i_am_def);
//System.out.println("i_am_pri= "+p.i_am_pri);
System.out.println("i_am_pro= "+p.i_am_pro);
System.out.println("i_am_pub= "+p.i_am_pub);
}
}

//Fourth Program
package Bangladesh;

public class TestBangladeshPackage
{
public static void main(String arg[])
{
Protection obj1=new Protection();
obj1.printClassInfo();
Derived obj2=new Derived();
UnderBangladeshPackage obj3=new UnderBangladeshPackage();
}
}

//--------------------------
//Fifth Program
package India;

class Protection2 extends Bangladesh.Protection
{
void Protection2()
{
}
void printMyInfo()
{
System.out.println("Package India only gets from Package Bangladesh------");
//System.out.println("i_am_def= "+i_am_def);
//System.out.println("i_am_pri= "+i_am_pri);
System.out.println("i_am_pro= "+i_am_pro);
System.out.println("i_am_pub= "+i_am_pub);
}
}
//Sixth Program
package India;

class UnderIndiaPackage
{
void UnderIndiaPackage()
{
}
void printMyInfo()
{
Bangladesh.Protection p=new Bangladesh.Protection();
System.out.println("Package under india accesses Package Bangladesh and gets only---");
//System.out.println("i_am_def= "+p.i_am_def);
//System.out.println("i_am_pri= "+p.i_am_pri);
//System.out.println("i_am_pro= "+p.i_am_pro);
System.out.println("i_am_pub= "+p.i_am_pub);
}
}
//Seventh Program
package India;

public class TestIndiaPackage
{
public static void main(String arg[])
{
Protection2 obj1=new Protection2();
obj1.printMyInfo();
System.out.println();
UnderIndiaPackage obj2=new UnderIndiaPackage();
obj2.printMyInfo();
}
}

//------------------------
//-----------------------
copy and pase these seven program in ur java editor and save them as corresponding class names and follow the follwing steps to run them:
step 1: compile first program and make a folder named "Bangladesh" and paste the .class file(not .java file) of ur program in the directory.
step 2: compile second and third program and transfer the .class files into the Bangladesh folder too.
step 3: compile fourth program and transfer the .class file into the bangladesh folder and then run it.

step 4: make another folder named India and move Bangladesh folder into India folder.
step 5: compile fifth and sixth program into the india program and leave the .class files in the same folder.
step 6: compile seventh program outside of ur india folder and transfer the .class file into the india folder and then run it.

Remember: only fourth and seventh program has main() method, so only these two will b able to run and show u some output to understand the basics. Hope these lucky seven program will help u understand what u need.

inform me abt ur success or !!!! :(
thanks
Tue Mar 30, 04 1:45 am
Back to top
Belal View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group