In Java method overriding can exist when there is Parent-Child classes and the child class provide specific implementation of a method that is also present in the Parent class.
Chair [ Parent class ] | int weight() [ Method of the Chair class ] | | [Armchair extends Chair] \/ Armchair [ Child class ] int weight() [ Overridden method ] In the Chair class this method weight() return 10 kilograms, but in Armchair return for example 15 ( 5 more for the arms)
This overriding come with several principals/limitations based on the Parent child relationship:
- Java Polymorphism will make sure that if you create object from the Parent class and execute the method then the parent method will be executed. If there is object created from the Child class then the Child method will be executed
- Final methods (method declared with final) can not be overridden.
- The child class overriding method can have the same or more method scope access but not less. If in the Parent class method has protected access scope then in the child class this method can only have protected or public access scope (but not private) . This way programmer can not hide overridden methods in the Child class.
- Private methods can not be overridden because private methods are only available in the class where they are used.
- Constructors can not be overridden, they are specific for the Class as they are called when the object from that class is created. So there is no point Overriding Parent constructor in the Child. However you can call parent constructor in child constructor body if needed.
- Overriding method in the Child class must have same return type (or subtype) as the parent.
- If the parent method does not have implementation and it is declared as abstract then it is obligatory in child method to override it and provide such implementation.
So for this example we will need 3 files. They can be in the same package.
Chair.java This is the parent method
// Our parent class. public class Chair { // Default implementation in the Parent method public int weight() { System.out.println(" --> [class Chair] [method weight] <--"); return 10; } }
Armchair.java This is the file with the child method
// Child class which extend Parent class public class Armchair extends Chair { // This is not mandatory annotation, but it is very helpful // This way you explicitly tell Java other developer and yourself // that you are overriding method from another class @Override public int weight() { System.out.println(" ==>> [class Armchair] [method weight overridden] <<=="); return 15; } }
Main.java Here Object from both or the Chair and Armchair classes are created and their methods “weight()” are called.
public class Main { public static void main(String[] args) { // Parent class Chair chair = new Chair(); // call weight from Parent class int chairWeight = chair.weight(); System.out.println("Weight of chair is: " + chairWeight); System.out.println("=================================="); // Child class Armchair armchair = new Armchair(); // call weight from child class int armchairWeight = armchair.weight(); System.out.println("Weight of armchair is: " + armchairWeight); } }
Output:
--> [class Chair] [method weight] <--
Weight of chair is: 10
==================================
==>> [class Armchair] [method weight overridden] <<==
Weight of armchair is: 15