| Author |
Question Regarding the Inheritance
|
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
Hi folks,
I have came across this question in an Interview and not able to answer, The Question is
I have a class A which has one() and two() methods, and I have two another classes B and C both extends A.
The Question is
For class B method one() should be accessible and two() should not be accessible
For class C method one() should not be accessible and two() should be accessible.
use access specifiers, put B and C in another package and so on... But the above condition should meet.
I have tried so many things not able to solve this,
Just wanted to know is it possible ?
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
If someone ask illogical question then you can reply answer in same way. .Condition satisfied.
|
Tell the difficulties that i am difficult.
|
 |
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
Hi Saloni,
I Really Appreciate the way you are thinking , Just wanted to confirm few things with you the method two() in class A is package or default specifier that means class on Another package cant override it, here comes an error
let me also try parallel, any new ideas are always welcome.
Regards
S.Sathiya
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Use private access modifier while you define two() in class A.It is working.cheers
|
 |
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
saloni jhanwar wrote:Use private access modifier while you define two() in class A.It is working.cheers
if you use private access modifier while you define two() in class A, will it satisfy the following statement ?
For class C method one() should not be accessible and two() should be accessible.
BTW I just got the answer that may be one of the way, just wanted to know different ways of thinking and innovative idea's from other people
Hint is I have not used final, static,protected keywords as a access specifiers for the methods. let me post the answer soon.
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
sakthi moorthy wrote:
BTW I just got the answer
I don't need magnifier.I can read.
sakthi moorthy wrote:
if you use private access modifier while you define two() in class A, will it satisfy the following statement ?
For class C method one() should not be accessible and two() should be accessible.
why you have any doubt?? class B and C should extends class A which is going here this condition satisfied and class C has method two you can check.Does this question state specifically that method two should be of class A ??? i think No.
and if you got answer then cheers happy ranching
|
 |
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
saloni jahnwar wrote:
why you have any doubt?? class B and C should extends class A which is going here this condition satisfied and class C has method two you can check.Does this question state specifically that method two should be of class A ??? i think No.
I think this is your consolidated answer and you are not going to edit it again
ok I will put my question in another way
Note in class B two() method is giving compilation error and class C one() method is giving compilation error
magnifier rules apply
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Did i override anything in my example??? this above is your code, you wrote this.Why are you asking to me about this ? you should ask to yourself that why you wrote crazy code what craziest thing you wrote above,i think you need much large magnifiers seriously,overriding depends on which access modifier you are using ok.cheers
Now see what i wrote and compare it with above your code
|
 |
chain singh
Ranch Hand
Joined: Feb 28, 2012
Posts: 116
|
|
Try this,,,,,,,,
[code=java]
class A
{
public void one() throws NullPointerException{ }
public void two() throws NullPointerException{ }
}
class B extends A
{
public void one() { }
public void two() throws Exception{} // compile time error
}
class C extends A
{
public void one() throws Exception {} // compile time error
public void two() {}
}
[/code]
|
 |
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
Ease up saloni jhanwar
For class B method one() should be accessible and two() should not be accessible
here accessible means i want to access the method one() of class A,So in class B if you override that method(one()) or
access that method(one()) on another method like following
For class C method one() should not be accessible and two() should be accessible.
above mentioned same is applicable for this statement also, but vise versa.
what craziest thing you wrote above
yeah i used to write craziest thing, because mistakes only makes us to learn perfect things
Thanks for the answers
chain singh
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
sakthi moorthy wrote:
BTW I just got the answer that may be one of the way, just wanted to know different ways of thinking and innovative idea's from other people
Hint is I have not used final, static,protected keywords as a access specifiers for the methods. let me post the answer soon.
Ok sorry,Now its time to post your answer which you got but did not post until now.I am eager to know how you did without final,static,protected keywords
|
 |
sakthi moorthy
Ranch Hand
Joined: Oct 17, 2007
Posts: 54
|
|
I am eager to know how you did without final,static,protected keywords
Still there is a draw back in my approach, anyhow i will share how i tried to solve this problem
I have made the class A as abstract and method one() as abstract.
|
 |
 |
|
|
subject: Question Regarding the Inheritance
|
|
|