aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding " Watch "Overriding " New topic
Author

Overriding

Shallu Shankar
Greenhorn

Joined: Apr 20, 2001
Posts: 15
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

Joined: May 05, 2000
Posts: 13974
private methods can not be overridden and are therefore not eligible for polymorphism.
------------------
Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
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

Joined: Apr 25, 2001
Posts: 4
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

Joined: May 05, 2000
Posts: 13974
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.
 
 
subject: Overriding