Overriding : Some important points to remember
When we think about inheritance, abstract class, interface and polymorphism then overriding always come in picture.
A simple effort to let you understand overriding :
Samsung releases note,note2,note3 type of series mobile and all these series have some same application but same application may behave differently in some aspect in different series.
So think at developer level what they might be doing. for a application to behave in note2 they must have written some methods inside a class, now in note3 they want same application but one behavior should be different, what a developer can do?
A developer can extend the class written for note2 , that will inherit the behavior written for note2 but for one change behavior they can override that method in note3 subclass and write logic for note3.
Now same application will behave little different in note3 due to overridden method.
Suppose there is a game in note2, now that game want to provide some new aspect in next release so that when you update the software you can avail that.
For this new update new developer is working on that now what he can do?
Does he need to open old code written by another developer and write new method there? He can do that but by doing this he will not take advantage of OOPs.
So here what he can do is, he can overload the method written by old developer with the requirement he want to provide in new updation.
Now when user will update his software he will get new functionality with old one still there.
There may different approach and way to achieve this.
Basic points need to consider while overriding:
1) First inheritance then only overriding. It is not possible to override without inheritance
2) We cannot override class field members, it can be only inherited.
3) Constructor of super class cannot be overridden is subclass but we can call constructor of super class from sublcass using super keyword
4) Only methods can be overridden.
5) In case of overriding, we can override abstract as well as concrete methods. Abstract methods meant to be being overridden unless and until class overriding the method is not an abstract class.
Points to consider when overriding any methods:
1) first method should be inherited then only it can be overridden
2) Method signature + return type should be same as superclass. Method signature means name of the method and argument list.
While overriding how compiler deals with the exceptions?
1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.
2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception
3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method
4) We can not override any final methods in subclass.
5) We can not override static methods , but when we write static methods in subclass with same signature as superclass then it is consider as method hiding.
6) Abstract methods must be overridden by concrete class. if any abstract class is extended by another abstract class then there is no boundation on overriding.