• 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

Overriding

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
For a program given below ...
public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}

Superclass() {
System.out.println("SuperClass Constructor Executed");
}

private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}

int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass {
Subclass() {
System.out.println("SubClass Constructor Executed");
}

protected int methodB() {
System.out.println("methodB in Subclass");
return 1;
}
}
The output for the above says that method B of base class will be called .
Just wanted the clarification for the same .
Thanks in advance ,
Shallu
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods can not be overridden and are therefore not eligible for polymorphism.
------------------
Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand a little on Thomas's explanation - A private method
cannot be overridden. The compiler will resolve at compile time
itself that the private method here will be bound to the
object of the base class. So when u invoke method B
in the private method, the base class method B is invoked.
You would get a similar output as in the case of a private
method if you make all the methods static.
So polymorphism does not apply methods which are static or
private.
Do correct me if I am wrong.
Regards
Sajida
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But as per Simon Roberts a private method in the subclass can override an private method in the Baseclass. Can you clarify.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thiru maran:
But as per Simon Roberts a private method in the subclass can override an private method in the Baseclass. Can you clarify.


This is incorrect. The private method in the sub class has not overridden the base classes method because that would imply that polymorphism is available and it isn't.
Think of it this way... in normal cases, which method is to be executed is determined at run time based on the actual object type. So if I do this:
Baseclass b = new Subclass();
b.method1();
method1 of the Subclass will be executed since at runtime, it will be determined that b is an object of type Subclass. However, if method1 is a private method in both Baseclass and Subclass, the compiler will determine that b.method1() must be referring to method1() of Baseclass because since method1() is private so it can not be overridden. Therefore it binds at compile time and method1() of Baseclass will be executed.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic