• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Santy Jagtap
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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");
}
}
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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 ]
 
Ravi Kishore
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santy Jagtap:
you mean to say private methods cann't be overridden?

That is correct.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!