Is it possible to override main method

Is it possible to override main method

Answer: No. main is a static method and you don’t need an instance of the class, so don’t need polymorphic behavior. You may hide it by defining another static main method in a subclass see example below:

package com.javahonk;

public class MainParent {

	public static void main(String[] args) {
		System.out.println("Parent");

	}

}

class MainChild extends MainParent {

	public static void main(String[] args) {
		System.out.println("Child");

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *