• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

2 questions on private methods !!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops...I misread the first question...it should be 'final'.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch...
No genius
Prabhu.
 
Hari Parthasarathy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic