• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
Shallu Shankar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private methods can not be overridden and are therefore not eligible for polymorphism.
------------------
Moderator of the Programmer Certification Forums
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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
 
Thiru maran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic