Author Topic:   2 questions on private methods !!
Hari Parthasarathy
greenhorn
posted April 26, 2000 09:39 AM             
1.If you want subclasses to access, but not to override a superclass member method, what keyword should precede the name of the superclass method? (This is #22 from MindQ mock test)..

2.Can a method declared as private in a class called Base be overridden by a method in a subclass of Base class without any access modifier specified ?

Some genius help me !!

Hari..

kking
greenhorn
posted April 26, 2000 10:01 AM         
For number 1, I think the word you are looking for is 'super'.

Have a look at the following code.

class PrivateBase{

private void someMethod(){
System.out.println("Base method called");
}

}
public class PrivateSub extends PrivateBase{
public static void main(String [] args){
PrivateSub a = new PrivateSub();
System.out.println("Hello");
a.someMethod();
//PrivateBase b = new PrivateBase();
//b.someMethod();
}
void someMethod(){
System.out.println("Sub method called");
}
}

[This message has been edited by kking (edited April 26, 2000).]

Indy
greenhorn
posted April 26, 2000 10:07 AM             
I think "final" is supposed to be put in front of the superclass method. the question doesn't say when "calling the superclass method", that means, in the superclass body.

please correct me if I am wrong.

Indy

kking
greenhorn
posted April 26, 2000 10:18 AM         
oops...I misread the first question...it should be 'final'.

Prabhu
greenhorn
posted April 26, 2000 10:57 AM             
For Q2, answer is 'Yes you can override a method (declared as private in a supercalss) with a method with no access modifier in the sub-class'.

Becasue you can always override a method to be more public...

No genius
Prabhu.

Hari Parthasarathy
greenhorn
posted April 26, 2000 12:11 PM             
Hello Prabhu,

public class Tester {
public static void main(String[] args) {
System.out.println(new Sub().g());
}
private int f() {
return 2;
}
int g() {
return f();
}
}

class Sub extends Tester {
public int f() {
return 1;
}
}

what is the output ??

Java Nut
unregistered
posted April 26, 2000 01:16 PM           
2.Can a method declared as private in a class called Base be overridden by a method in a subclass of Base class without any
access modifier specified ?

This is a poorly phrased question. A method declared as private in Base cannot be seen by a subclass, therefore, there is no method to 'override'! A method with the same name in a subclass would have no restrictions on it based on inheritance alone!

The answer as the question is worded, ultimately is 'no' because a private method cannot be overridden.


Eric Barnhill
ranch hand
posted April 26, 2000 09:48 PM         
I agree with Java Nut...you can have a public method with the same return type but since you can't see the private method in the first place, you're not overriding it. If that's a mock q. it seems like a candidate for errata...


Eric B.

maha anna
bartender
posted April 27, 2000 07:46 AM             
Hello all,
The answer for the above post by Hari is final. And the MindQ Mock Exam results also say the same.

I wanted to post this just to make things clear.

regds
maha anna

|