filmov
tv
Method overriding in java with respect to static methods and exceptional handling classes

Показать описание
You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.
Can we override the static methods?
No, we can't override the static methods or the concept of overriding is not possible with the static methods. And, when you try to do the overriding with the static method, you will get the following error.
CE Error:-
===========
public void m1()
^
overridden method is static
1 error
Program :-
=========
class P
{
public static void m1()
{
}
}
class C extends P
{
public void m1()
{
}
}
class Test
{
public static void main(String[] args)
{
}
}
Why static methods cant be overridden?
Static methods are the class level methods and the non static methods are the object level methods, and their memory areas are different, so you cant achieve the concept of overriding with the static methods.
The concept of method overriding is possible only with non static methods or instance methods.
When you use the static methods of same return type, same access modifier and the same signature, the method resolution happens at the compilation time and the concept of method hiding gets introduced.
Method overriding with respect to java exceptions?
Please be informed that there are no issues with the unchecked exceptions in the overriding concept. However, when you declare a throws keyword with the respective checked exceptions, it is mandatory to ensure that the child class throws the same exception as the parent class or the child class exception or nothing.
Can we override the static methods?
No, we can't override the static methods or the concept of overriding is not possible with the static methods. And, when you try to do the overriding with the static method, you will get the following error.
CE Error:-
===========
public void m1()
^
overridden method is static
1 error
Program :-
=========
class P
{
public static void m1()
{
}
}
class C extends P
{
public void m1()
{
}
}
class Test
{
public static void main(String[] args)
{
}
}
Why static methods cant be overridden?
Static methods are the class level methods and the non static methods are the object level methods, and their memory areas are different, so you cant achieve the concept of overriding with the static methods.
The concept of method overriding is possible only with non static methods or instance methods.
When you use the static methods of same return type, same access modifier and the same signature, the method resolution happens at the compilation time and the concept of method hiding gets introduced.
Method overriding with respect to java exceptions?
Please be informed that there are no issues with the unchecked exceptions in the overriding concept. However, when you declare a throws keyword with the respective checked exceptions, it is mandatory to ensure that the child class throws the same exception as the parent class or the child class exception or nothing.