overriding

hi,

in my superclass, i have a method called void protected print(),

this is strange but in my subclass, i am able to overide the protected method with either protected and public.

public class parent{

protected void print(){
System.out.println("In parent");
}
}

public class child extends parent{

public void print()
{
System.out.println("In child");
}

}

however, in parent i have public print and i try to override it protected, i get error: "print() in child cannot override print() in parent; attempting to assign weaker access privileges; was public
zizi21Asked:
Who is Participating?
 
basicinstinctConnect With a Mentor Commented:
if you override a method you are not permitted to make it less 'visible'... so if it is public in the superclass you can't make it protected (because protected is less visible than public).  If it is protected in the superclass you can go to public in the subclass because you are making it more visible.
0
 
zizi21Author Commented:
thank you very much.. Studying for exam and all this is helping a lot . God bless.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.