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 are the differences between include directive and include action?

1180


how we can create packages in java?

955


How do you sort words in java?

922


Where is core java used?

1008


Is main a function?

1054


Define jre i.e. Java runtime environment?

1006


What is the different types of functions?

1057


What is a conditional equation?

1041


How do I enable java in safari?

1028


What is tochararray in java?

934


How do you calculate square roots?

1161


Explain the transient field modifier?

1043


What is meant by flickering?

1079


What is defined as false sharing in the context of multithreading?

1043


What is the map interface in java programming?

1015