• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per the methodd overriding rules the derived class can use the same access or restrict the acess of the method. but in case of interface y will it show compiler error of weaker access
for ex:

but as per the overrideing rules the derived class can restrict the access of move () but why is it showing compilation error of weaker access??
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in which resource did you find these method overriding rules? Because that's completely wrong! An overriding method must have the same access modifier or less restrictive.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:as per the methodd overriding rules the derived class can use the same access or restrict the acess of the method. but in case of interface y will it show compiler error of weaker access
for ex:

but as per the overrideing rules the derived class can restrict the access of move () but why is it showing compilation error of weaker access??


Hi Meghana,

All the rules of the java programming language are easy to mix up. You have the overriding concept backwards. An overriding method can specify the same access modifier or less restrictive. So, if the base class specifies a method with protected access then the overriding method can either itself be protected or public.

Remember the access modifiers from least restrictive to most restrictive is: public, protected, default (no access modifier), private.

Interfaces are especially tricky, because the members of these components have implicit values that are there regardless of whether they are explicitly defined in the source code. For instance interface members are always public static and final and methods are always public and abstract. The only access modifier allowed for a class overriding an interface method is public since public is the most open access modifier.

I hope this helps some. This is used to confuse me all the time. I am sure you will understand with some more studying.

Respectfully,

Ted
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ted for the explanation....
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:Thank you ted for the explanation....


You are welcome.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:Thank you ted for the explanation....

May I assume your understanding was incorrect or you mixed up with the rules for overloading? And the resource you are studying from did mention the correct rules for overriding? Otherwise it would be great for other ranchers studying for this certification to mention which resource mentions incorrect rules for overriding.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please check out this excellent handy resource from the amazing tutorials point website: overriding rules for Java

I hope this helps. It helped me remember how an overriding method can not throw any broader checked exceptions but can throw any errors or runtimeExceptions.

Regards,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic