• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

overriding and marker interface questions

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recently I attended an interview and could not answer these two questions satisfactorily:

1. The access level in the overriding method can't be more restrictive than the overridden method's. Why ?
2. A real time example of marker interface?

I would be glad if someone could answer these so that I can build my knowledge.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think yourself the answers to these questions would be? Some hints to get you started:

Question 1: Suppose that you have a superclass with a public method, and a subclass in which that method is overridden, but which is protected. What could go wrong? Could code that shouldn't be able to call the protected method suddenly call it? How?

Question 2: What is a marker interface? Are there examples of marker interfaces in the standard Java library? What are they used for?
 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Finding it tough to think

2) I know of Cloneable and Serializable. These give information to the JVM to treat the classes (marked with the marker interface) in a certain way. But the interviewer was more interested in some real time example where I can use marker interface.
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP seems to have given this some thought, so I going to give my thoughts on the question,

OP --> 1. The access level in the overriding method can't be more restrictive than the overridden method's. Why ?
Jesper --> Question 1: Suppose that you have a superclass with a public method, and a subclass in which that method is overridden, but which is protected. What could go wrong? Could code that shouldn't be able to call the protected method suddenly call it? How?

Jesper has almost answered your question with his hint.

However due to polymorphism the method A() in the Child class which is protected gets called. This should not be allowed.

Got to rush, answer to Question 2 soon
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic