Can we Override a main() method in Java?

preview_player
Показать описание
We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.

Following are some important points for method overriding and static methods in Java.
1) For class (or static) methods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.

2) For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.
Рекомендации по теме
Комментарии
Автор

very nicely explained. Very informative video specially for interviews !!!👍👍

suddharghyasom
Автор

Static methods does not require objects to be created. Still method hiding is based on reference types.

sarin
Автор

Bro the actual method called is based on the object type, not on the reference type, the methods which are allowed to be called is based on reference type, but the actual method called is from the object type

Please clear your concepts first

Parent method is called because the object is of Parent class, not because reference is of type Parent

Reference type tells what methods I can call, but the actual method called is that of the object created on the heap

ankursharma
welcome to shbcf.ru