• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Urget Plzzzzz Help - Lot of confusion.

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Was taking SCJP5 Mock Tests supplied by Kathhy's book.Most of the questions were of these sort,

Class A{

int getXXX(){
}
}

Class B extends A{
// Insert some crap.
}

The method getXXX is clearly having default Modifier. So Class B will not extend that method (dosent come under the inheritance chain).But the answers that I have gone through are

1.protected int getXXX() - becoz protected is not more restrictive
2.final int getXXX() - becoz final modifier can be applied

and both the above "overide" the method.


Plzz help. It would be great if Katty is reading this mail.

Thanks
Kishore
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kishore

The method getXXX is clearly having default Modifier. So Class B will not extend that method (dosent come under the inheritance chain).



If Class B and Class A are in same package then class B will inherit getXXX() method. If a method is marked private it will not be inherit.


1.protected int getXXX() - becoz protected is not more restrictive


Yes this is a legal override of the method.


2.final int getXXX() - becoz final modifier can be applied



Also this is a legal override of the method.

What excatly is your question?
Both the answers that you have given here are correct.
Thanks
Deepak
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can You help me , i have K&B book and i gave one of my friend this book for prepation on returing the book she lost softcopy so i dont have any soft copy of the mock test whic is provided in this book , if you are having this book mock test softcopy can you send me in the mail because you are also preparing from mock test

Thanking you
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

"kishore kishore" wrote:

The method getXXX is clearly having default Modifier. So Class B will not extend that method (dosent come under the inheritance chain).



Your first posting here?


Welcome to the Ranch!



What are you meaning with class B not extending that method?
Clearly class B inherits that method.

If you want to override it, visibility cannot be reduced. The method in A has got package visibility.
With protected the visibility is higher, as it is visible not only in the same package but also in classes in other packages that extend A.
I admit that the terminus protected is a bit misleading as one may think the visiblilty may be lower than default.



Only one small issue "kishore": The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.

Regards,
Bu.
 
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic