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
What are the differences between include directive and include action?
how we can create packages in java?
How do you sort words in java?
Where is core java used?
Is main a function?
Define jre i.e. Java runtime environment?
What is the different types of functions?
What is a conditional equation?
How do I enable java in safari?
What is tochararray in java?
How do you calculate square roots?
Explain the transient field modifier?
What is meant by flickering?
What is defined as false sharing in the context of multithreading?
What is the map interface in java programming?