Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

calling overideen method in constructor

814889Jan 6 2012 — edited Jan 8 2012
Hi ,

I want to know why is the overiden method of subclass is called from super class constructor,below is the code
import java.util.*;

class Super{
	// Broken - constructor invokes an overridable method
	public Super(){
		overrideMe();
	}

	public void overrideMe(){

		System.out.println( " in overide me of super class" );
	}

}

public final class Sub extends Super{
	private final Date date; // Blank final, set by constructor

	Sub(){
		date = new Date();
	}

	// Overriding method invoked by superclass constructor
	//
	//
	@Override
	public void overrideMe(){
		System.out.println( " in overide me of subclass" );
		System.out.println( date );
	}

	public static void main( String[] args ){
		Sub sub = new Sub();
		sub.overrideMe();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 5 2012
Added on Jan 6 2012
16 comments
504 views