Wednesday, April 23, 2014

Object Programming language Part7 - Overriding

Overriding
When we code on basis of inheritance, abstract class,interface and polymorphism then we think and implement overriding concept.

The dictionary meaning of overriding is to counteract the normal operation going on.
There are certain rules that we need to keep in mind while using overriding concept:
  1. First inheritance then only overriding. It is not possible to override without inheriting.
  2. We can not override class field members,it can be only inherited.
  3. Constructor of super class can not be overridden is subclass.
  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.
Rules to consider while overriding any method:

Imp: first method should be inherited then only it can be overridden
  1. Method signature + return type should be same as superclass. Method signature means name of the method and argument list.
  2. There is some exceptional rule in terms of return type that is called as co-variant return type that states that return type of overriding method  can be subtype of the return type declared in method of superclass.
  3. As there is always an access modifier attached with method so in that case overriding  method(method of subclass) can not be more restrictive than the overridden method(method of super class). Although it can have any less restrictive access modifier. ( why? i will explain in sample code)
  4. In case of subclass and superclass define in same package, we can override all those methods which are not private,final and static.
  5. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.
As methods can throw exception so there are some specific rules about exception in terms of overriding. ( i will explain exception in upcoming post). Till now think exception as some exceptional condition that has arisen in your code that can not be handled by your Jvm or Compiler.

Rules need to be consider in terms of exception while overriding any method
  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.
As in OOPs there is also concept of polymorphism and in case of polymorphism the accessibility of overriding methods adds some more concept that i will Explain using codes then only it will easy to understand. 

To understand these all points in term of code follow my next post: Practical 5th - Demo of Overriding
                                                                                                 --keep visiting

No comments:

Post a Comment