• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding access visibility rules

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a super class given method
public void aMethod(){}

In a subclass giving the only visibility is
public void aMethid(){}

Like that,
In a Super class,a method with protected---subclass,a method with protected/public is valid.giving private or default is error

In a Super class,a method with private--no overriding in subclass

In a Super class,a method with default-subclass,a method with protected/public/default is valid. giving private is error

I checked every of the above with my compiler.still I need to confirm from anyone because consider the following question.


Given the code below,and making no other changes,which access modifiers ar placed before myMethod() on line 3

class Super{
void myMethod(){//line 3

}
}

class Q extends Super{
void myMethod(){//line 7

}
}

Given options:
1.public
2.protected
3.private
4.none of the above

Given 1,2,3 answers are correct.since private feature is accessible only to its own class,selecting private is also valid.

But my answer is only giving private on line 3 would work.of course ,line 7 is not overridint the super class method.that is a separate method.How come giving public/protected on line 3 is valid for the above program.please help me and correct me if my beginning rules are not correct.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are allowed to increase the visibility of overridden methods, but you can't decrease the visibility.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply keith. is the three answers correct pr wrong?.....please give more details depends on the above program
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given answer 1,2,3 is incorrect; only line 3 is correct, for the reason you gave. Wherever you got this question from, it's not a trustworthy source; ignore it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic