• 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

override of private method

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In whizlabs Exam Simulator I have found one question:

A private method may be overridden by a private, friendly, ____, or public method.

I needed to fill in the gap. Here could be only one right answer - protected, but, if i'm not mistaken, private methods ARE NOT overridable? Am I wrong?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private methods are not inherited and, therefore, can not be overridden.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question on the mock exam is worded incorrectly so the other post is correct in saying that nothing could answer the question correctly. However, in case you are wondering the question is trying to say that while the private method cannot be overidden, because it is not inherrited, it can be redefined. So the correct statement would be that a private method can be redefined as a public, friendly (default), protected, or private method. However, for the real exam remember that this method is not actually overidden (despite how it looks), and polymorphism does not apply.

Hope this helps!

Bobby
SCJP
 
Vlad Golodov
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly I'm on the same opinion.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vlad,

The following is the code which could explain u the concept that private methods could not be overriden.

class overridePrivateClass
{
int data;
private void func()
{
System.out.println(data);
}
}
class derivedClass extends overridePrivateClass
{
void func()
{
System.out.println(super.data);
}
}
class overridePrivate
{
public static void main(String [] args)
{
derivedClass ob = new derivedClass();
ob.func();
}
}

Here, overridePrivateClass has got a private method func.
From the code u might say that in the derived class " derivedClass"
the function func has been overriden, but actually the function is
not overriden in derivedClass. The func in overridePrivateClass is
not vissible in derivedClass, since it is declared private.
And when u enter in derivedClass , the is no definition for func and
so we could write our own code for that function.

I hope it could help u.

Arnab
 
Vlad Golodov
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you all!

Maybe this code will explain more clearly

public class PrivateMethodTest extends PrivateMethodClass{
public void method(){}
public static void main(String[] args){
PrivateMethodClass mClass = new PrivateMethodTest();
mClass.method();
}
}

class PrivateMethodClass{
private void method(){}
}

Here is the almost the same situation as before, in my opinion. Private method is not visible outside.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods are not visible outside. so we cannot override.
 
reply
    Bookmark Topic Watch Topic
  • New Topic