Can we override the main method?
Answers were Sorted based on User's Feedback
Answer / sadikhasan palsaniya
Yes we can override main method that explain by following Code.
public class DemoOverride {
public static void main(String string[]){
System.out.println("I am in Main method of super class");
String str[]={"sadik","palsnaiya"};
Over1.main(str);
Over2.main(str);
}
}
class Over1 extends DemoOverride{
public static void main(String str[]){
System.out.println("I am in Main method of Middle
class");
}
}
class Over2 extends Over1{
public static void main(String str[]){
System.out.println("I am in Main method of Child class");
}
}
| Is This Answer Correct ? | 8 Yes | 1 No |
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 |
Answer / nikhil agrawal
Main method is static and static method is not override.
For example:- if i have Class A having main method and Class B extends Class A and also having the main method. in that case Class B hide the main method of A not override.
overriding process occur on run time and but static method load on compile time. that's its the reason we can not override main method but we can hide it.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / gautam kumar
no we cannot override main(),becoz it is defined staticly .
| Is This Answer Correct ? | 0 Yes | 2 No |
What is sizeof in java?
What is object class in java?
What is size_t?
What are the types of web technologies?
Which class has no duplicate elements?
What is difference between Heap and Stack Memory?
If I have 1000 objects and my requirement is to sort them quickly, then which collection would you recommend and why?
why we cannot declare static variable inside a static method
What data type is string java?
all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.
Which methods are used during serialization and deserialization process?
byte a=5; byte b=5; byte c=a+b; System.out.println(c); whats the o/p?