Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

what is the difference between the "protected and default"
modifiers?

Answer Posted / waheeb

we cannot make a class protected. however it is observed
that both default and protected modifiers exhibit same
characteristics. both are not accessible outside the
package and are accessible within the same package whether
the class is inheriting from the superclass or not.

for example:
package pack1;

public class SuperClass {
public int publicvar;

protected int protectedvar;

private int privatevar;

int defaultvar;
public void publicmethod() {
System.out.println("Public Method");
}

protected void protectedmethod() {
System.out.println("Inside Protected
Method");
}

private void privatemethod() {
System.out.println("Inside Private Method");
}

void defaultmethod(){
System.out.println("Inside Default Var");
}
}

class subclass extends SuperClass{
public static void main(String args[]){
SuperClass obj = new SuperClass();
obj.protectedvar = 10;
obj.defaultvar = 10;
obj.defaultmethod();
/*
protected and default feilds accessible in
the same
*/
}
}


class anotherClass{
public static void main(String args[]){
SuperClass obj = new SuperClass();
obj.protectedvar = 10;
obj.defaultvar = 10;

/* protected and default feilds accessible
in the same
package without extending*/

}
}

Is This Answer Correct ?    0 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by structural programming?

974


What is the difference between the final method and abstract method?

999


What is the purpose of sizeof operator?

988


Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me

1815


Similarity and difference between static block and static method ?

882


Which types of exceptions are caught at compile time?

986


What is a for loop in java?

1001


Why is java architectural neutral?

1068


What are the java ide's? Explain

974


Give reasons supporting that string is immutable.

896


Is sizeof a preprocessor?

951


What are the two basic ways in which classes that can be run as threads may be defined?

1022


What is the maximum size of list in java?

918


What is the difference between interface & abstract class?

993


What are the characteristics of Final,Finally and Finalize keywords.

1133