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

Why do we create threads in java?

1141


How is final different from finally and finalize?

863


What is data movement?

1066


How do you reverse a string in java without using string buffer?

1039


When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

998


What is a dynamic array in java?

1096


Does windows 10 need java?

1004


Which sorting is best in java?

958


How do you sort arrays in java?

878


How many bytes is a string java?

964


Is zero a natural number?

974


What is the difference between static and global variables and also define what are volatile variables?

987


What is the current version of java?

976


What are different types of constants?

964


What is the purpose of using the java bean?

998