Monday, 23 May 2016

Method Overriding and Virtual Method Invoking

The purpose of overriding is to allow sub-class to carry out the method in its own preferred way. So the same behaviour may differ from parents to its derivatives. It is an OOP feature that enables a derived class to define a specific implementation of exiting base class to extend its own behaviour. 

Overriding is related to the concept of inheritance; 
A constructor cannot be inherited. A static method is not inherited, but accessible for all instances. A final method cannot be inherited. So we don't need to talk about how to override them. Only non-final instance method that can be inherited, that is overriding about. 

Method overriding reuses the method signature and using the same argument list.  Overridden methods are also synonymous with polymorphic methods. 

The static method cannot be overridden, but hidden; so the concept of overriding is only related to the inheritance concept.  
A final method cannot be overridden. 

A method that can be overridden by a derived class is called a virtual method. Please note that the virtual method is not a Java term, but a term used in OOP domain. A virtual method invocation refers to invoking correct overridden methods. 

parts of a method declaration
non-access modifier  ¶  access modifier  ¶  return type¶  method name ¶ parameter list ¶ exception list¶ 
static/final                  ¶  public, protected
synchronised              ¶  default, private

Correct syntax of overriding methods

method signature exactly the same
method argument: exactly the same
access modifier: can be the same level; or less restrictive access level
Non-access modifier: overriding method can use any non-access modifier. 

Return type: covariant return types.  the overridden method cannot return more generic type; otherwise, the client cannot determine what can be returned from the overridden method.

what is the co-variant principle?
supertype and sub-type

Exceptions that are thrown: for the checked exception, the overridden method can throw the same or none exception; or a subtype of exception that is thrown by the overridden method.  An overriding method can throw any runtime exception, even the overridden method doesn't. 
this is because the un-checked exception is not part of method composition, so it is not checked by the compiler.  

The overriding method can throw no exception, the same exception or subtype of the exception that is thrown by the overridden method. 


It is the reference variable type that dictates which overloaded method will be chosen at the compilation time. 

Can you override all methods from the superclass? 
No. you cannot override static method; you cannot override private method; you cannot override the final method. So you cannot override all methods from the superclass. 

A Method can be overloaded, overridden, or hidden.
A Static method cannot be overridden but hidden. Hidden is about the scope. 
Overloaded is about reuse the method signature, but different parameter list;  
Overriding is about invoking virtual method; 

Why overriding Class Object is important



Overriding has many rules in Java.

Overriding happens only with the concept, Inheritance. Our parents prefer to celebrate a new year in their way; however, for the young generation, they may celebrate in a different way. In such a case, a method inherited can be overridden in the child class. It is called the overridden method, and its counterpart on the child class is called overriding method.

(1) using the same signature.
(2) using the same argument.
(3) allowing co-variant return types. (since Java 1.5)
(4) An overriding method cannot throw a superclass exception, while a constructor of a subclass cannot throw subclass exception(must be the same).

private/final/static methods cannot be overridden.

private cannot be inherited so won't be overridden;
a final method is not allowed to be inherited.
a static method can be hidden, but not overridden.

(5)about access modifier: you cannot define more restrictive access modifier than the method that is going to be overridden.
(6) if the overridden method does not have any throws clause, the overriding method cannot declare any checked exceptions; can throw unchecked. 



No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...