Can we override the main method?

Answer Posted / eknath wagadre

I think Sadikhasan Palsaniya answer is wrong....

Well... the answer is NO!!!! Why it's No...

if you think from the perspective of how an overriden method
should behave in Java. But, you don't get any compiler error
if you try to override a static method. That means, if you
try to override, Java doesn't stop you doing that; but you
certainly don't get the same effect as you get for
non-static methods. Overriding in Java simply means that the
particular method would be called based on the run time type
of the object and not on the compile time type of it (which
is the case with overriden static methods). Okay... any
guesses for the reason why do they behave strangely? Because
they are class methods and hence access to them is always
resolved during compile time only using the compile time
type information. Accessing them using object references is
just an extra liberty given by the designers of Java and we
should certainly not think of stopping that practice only
when they restrict it :-)


Example: let's try to see what happens if we try overriding
a static method:-


class SuperClass{

......

public static void staticMethod(){

System.out.println("SuperClass: inside staticMethod");

}

......

}


public class SubClass extends SuperClass{

......

//overriding the static method

public static void staticMethod(){

System.out.println("SubClass: inside staticMethod");

}


......

public static void main(String []args){

......

SuperClass superClassWithSuperCons = new SuperClass();

SuperClass superClassWithSubCons = new SubClass();

SubClass subClassWithSubCons = new SubClass();


superClassWithSuperCons.staticMethod();

superClassWithSubCons.staticMethod();

subClassWithSubCons.staticMethod();

...

}


}


Output:-


SuperClass: inside staticMethod

SuperClass: inside staticMethod

SubClass: inside staticMethod


Notice the second line of the output. Had the staticMethod
been overriden this line should have been identical to the
third line as we're invoking the 'staticMethod()' on an
object of Runtime Type as 'SubClass' and not as
'SuperClass'. This confirms that the static methods are
always resolved using their compile time type information only.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you make a constructor final in Java?

627


What is the scope or life time of instance variables?

656


How do you remove an object from an arraylist in java?

463


In java how do we copy objects?

549


What is a default constructor and also define copy contrucyor?

601






What are the ways to instantiate the class class?

596


How do you achieve singleton?

529


How can you make a class serializable in java?

567


Can an interface extend another interface?

596


Is main a function?

513


Does java list allow null?

546


why doesn't java run on all platforms?

567


Explain about interthread communication and how it takes place in java?

546


Can bool be null?

522


What is getkey () in java?

575