Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

overriding

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();

please explain the O/p .method S1 is overrideen or not the explanation of this question says it is not overriden and does not giv any error .Please explain ???
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:
method S1 is overrideen or not

Whatever you do, private methods will never be overwritten.
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what will be the output of this piece of code???can any one run the code and then explain???
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:
so what will be the output of this piece of code???

What would you expect as output?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is o/p rt?
R.printS1
S.printS2
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankith suresh:
R.printS1
S.printS2

And why?
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah ankith correct !!! please expain
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(others pls correct me if wrong, but...)

The reason the output is: R.printS1 S.printS2

"R.printS1" prints because the instantiated class S (a subclass of R) calls the protected printS1S2 method of R.

"S.printS2" prints because the printS2 method of S overrides the printS2 method of R.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Sandman:
(others pls correct me if wrong, but...)

The reason the output is: R.printS1 S.printS2

"R.printS1" prints because the instantiated class S (a subclass of R) calls the protected printS1S2 method of R.

"S.printS2" prints because the printS2 method of S overrides the printS2 method of R.



This is correct. These facts should explain the confusion a bit

1. R#printS1() is not overridden by S.
"R.printS1" will print because as defined in the superclass R, printS1() is a private method, not a protected one. Any method that is of private access can't be seen by its subclasses, and therefore can't be overridden (nor hidden, nor overloaded). As far as each class is concerned, they each just happen to have private helper methods with the same signature.

2. S#printS2() successfully overrides R#printS2
Why? Because R#printS2 is a protected member, and therefore inherited by S. Because it isn't final or static, S overrides it without issue and the behavior of the method for all instances of S() will invoke S#printS2() when calling printS2().

3. R#printS1S2 is never overridden at all
Because printS1S2's implementation only exists in R, any non-overridden methods will be invoked just as they would if the instance's most specific type was R.

Based on the third observation, you can see that what's really happening under the covers when:



are invoked is:



What the compiler sees is--from the context of printS1S2() in R--a private method in R and an instance method of R overridden by S.

That's my stab at explaining why it will read "R.printS1 S.printS2"
[ July 11, 2007: Message edited by: Justin Searls ]
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic