The access in modifiels are:
public
private
protected
default access (no access modifier)
By using access modifiers you can control access to methods and variables.
Access modifier
Assessible from here
public
inside and outside the class
private
only inside the class
protected
from anywhere in the same package, in sub class in any package
default (no access modifier)
from anywhere in the same package
Can a class inherit a private method
Will this code compile? If not why not?
// in Base.java
package oca ;
public class Base {
private void low (){
}
}
// in Sub.java
package oca ;
public class Sub extends Base {
void now () {
low ();
}
}
Can a class inherit a protected method
Will this code compile? If not why not?
// in Base.java
package oca ;
public class Base {
protected void low (){
}
}
// in Sub.java
package oca ;
public class Sub extends Base {
void now () {
low ();
}
}
Can a class inherit a default access level method in the same package?
Will this code compile? If not why not?
// in Base.java
package oca ;
public class Base {
void low (){
}
}
// in Sub.java
package oca ;
public class Sub extends Base {
void now () {
low ();
}
}
Can a class inherit a default access level method from a class in a different package?
Will this code compile? If not why not?
// in Base.java
package oca ;
public class Base {
void low (){
}
}
// in Sub.java
package oca .pkg ;
public class Sub extends Base {
void now () {
low ();
}
}
Can a class inherit a protected method from a class in a different package?
Will this code compile? If not why not?
// in Base.java
package oca ;
public class Base {
protected void low (){
}
}
// in Sub.java
package oca .pkg ;
public class Sub extends Base {
void now () {
low ();
}
}