• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

What output do you expect and WHY about the following code?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What output do you expect and WHY?
************************************

class BAAP
{
private void meth()
{
System.out.println("Baap");
}
public static void main(String[] args)
{
BAAP b = new BETA();
b.meth();
}
}
class BETA extends BAAP
{
private void meth()
{
System.out.println("Beta");
}
}
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output should be "baap" since the method "meth()" is private and hence can't be overriden.
 
Santy Jagtap
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean to say private methods cann't be overridden?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private method is not visible to the child then how can it be overridden ?

Try to change the method as a a protected method and check the output.
[ April 06, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also u cannot change the method in BAAP to public and have the childs method as private..
 
Santy Jagtap
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods can be overridden
execute the following code
class BAAP
{
private void meth()
{
System.out.println("Baap");
}
public static void main(String[] args)
{
BAAP b = new BETA();
b.meth();
}
}
class BETA extends BAAP
{
//private void meth()
public void meth()
{
System.out.println("Beta");
}
}

isnt it an example of overriding?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the lapse ...the first time i ran it gave me error

But he second time it was fine...
 
Santy Jagtap
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know the output that it'll call baap's method but just want to know why this is so?
Any full proof answer?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to change both the method as a protected and check the output. You 'll come to know.

See ,
you cannot override a private method even thought it may appear that you are overriding it - A private method is restricted to the class in which it is defined as it cannot be inherited.
[ April 07, 2005: Message edited by: Srinivasa Raghavan ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi santy,

OUTPUT:

BAAP

the reason is as follows.

the main is written in BAAP, so it is a member of BAAP. The follwing statements:

BAAP b = new BETA();
b.meth();

observe carefully that meth() in BETA is private so it can not be assessed out side the class, so the method "meth()" of the superclass (BAAP) is called as main is a member of BAAP class which is valid.
 
Santy Jagtap
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the perfect answer the answer is as follows

whenever we access any private method or private variable of a particular class from the same class it will call the same class's private method only irrespective of inheritance.

for supporting my answer giving one example

class BAAP
{
protected void meth()
{
System.out.println("Baap");
}
public static void main(String[] args)
{
BAAP b = new BETA();
b.meth();
}
}
class BETA extends BAAP
{
//private void meth()
public void meth()
{
System.out.println("Beta");
}
}

meth method is overridden in BETA with higher access specifier so now as per inheritance rule (since meth of BETA is accessible)output would be : Beta
but since meth is private in BAAP the output will be : Baap

for more clarification just change the access specifiers of both the method as public and observe the output.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Santy Jagtap:
you mean to say private methods cann't be overridden?


That is correct.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Specifically, private methods cannot be overridden; they do not participate in polymorphism. However, they can be shadowed; that is, the subclass can define its own method with the same signature. This looks like overriding, but it isn't -- as you example shows, polymorphism does not play a role here.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Santy Jagtap:
private methods can be overridden
execute the following code

isnt it an example of overriding?



No this is not an example of overriding. This is called shadowing. As someone else explained, the biggest difference is that the private version in BAAP cannot be called through polymorphism on an instance of BETA.

Layne
 
Get me the mayor's office! I need to tell her about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic