In this article, you are going to learn ‘Overriding in Java Programming Language’. Overriding simply means ‘to override an existing phenomenon’.
In java programming language, overriding is the process of using the parent class method with the same number of parameters or arguments in its subclass so as to provide the same functionality to the overridden methods.
Also Read: Interfaces in Java Programming Language
OVERRIDING FUNDAMENTAL PRINCIPLES
- The parameters used in the overridden method in the subclass must be the same as the method parameters in the parent class.
- To override a method, it is necessary to inherit a parent class by a child class.
- It is impossible to override a method that is declared final.
- It is impossible to override a method that is declared with a static keyword.
- It is impossible to override constructors.
- Same access modifier should be used in both the parent class method and overridden method.
- Overriding best defines the Runtime Polymorphism, an object-oriented programming concept.
PICTORIAL REPRESENTATION OF METHOD OVERRIDING
A pictorial representation of Method Overriding is shown below. Here, three child classes ‘Digital Marketing’, ‘PHP’ and ‘Web Designing’ have the same parent class ‘W3TrainingSchool’. Also the method courseFee() is same in the parent class and its three child classes, however, the return value is different in all the cases.
It means the same methods have been used to return different values. This clearly shows that we can use the same parent class method to override the methods in the child class to show similar kind of behaviors but having different return values.
OVERRIDING USING INSTANCE METHODS
Example:
class W3TrainingSchool {
public int courseFee () {
return(0) ;
}
}
class DigitalMarketing extends W3TrainingSchool {
public int courseFee () {
return(50000) ;
}
}
class PHP extends W3TrainingSchool {
public int courseFee () {
return(30000) ;
}
}
class WebDesigning extends W3TrainingSchool {
public int courseFee () {
return(45000) ;
}
}
class MethodOverriding {
public static void main(String args[]) {
W3TrainingSchool obj1 = new DigitalMarketing ( ) ;
System.out.println(“Fee of Digital Marketing =” + obj1.courseFee ( )) ;
W3TrainingSchool obj2 = new PHP ( ) ;
System.out.println(“Fee of PHP =” + obj2.courseFee ( )) ;
W3TrainingSchool obj3 = new WebDesigning ( ) ;
System.out.println(“Fee of Web Designing =” + obj3.courseFee ( )) ;
}
}
Output:
Fee of Digital Marketing = 50000
Fee of PHP = 30000
Fee of Web Designing = 45000
OVERRIDING USING INTERFACE METHODS
Overriding can also be achieved using abstract and default methods of an interface. Following are the two cases with the use of default methods while overriding them in a java program.
Case 1: If the instance methods of a class and default methods of an interface are overridden, then preference will be given to the instance methods rather than the default methods.
Example:
class Family {
public String familyMembers( ) {
return “This is a Family” ;
}
}
interface Father {
default public String familyMembers( ) {
return “Father” ;
}
}
interface Son {
default public String familyMembers( ) {
return “Son” ;
}
}
interface Daughter {
default public String familyMembers( ) {
return “Daughter” ;
}
}
class Mother extends Family implements Father, Son, Daughter {
public static void main(String [] args) {
Mother obj = new Mother( ) ;
System.out.println(obj.familyMembers( )) ;
}
}
Output:
This is a Family
Clearly, preference has been given to the instance method as the string ‘This is a Family’ is returned.
Case 2: The overridden interface default methods will be ignored.
Example:
interface Family {
default public String familyMembers( ) {
return “This is a Family” ;
}
}
interface Son extends Family {
default public String familyMembers( ) {
return “Son” ;
}
}
interface Daughter extends Family { }
class Mother implements Son, Daughter {
public static void main (String [] args) {
Mother obj = new Mother( ) ;
System.out.println(obj.familyMembers( )) ;
}
}
Output:
Son
Clearly, the default method in the interface Family is overridden in the interface Son. Therefore, it is ignored and the returned output is ‘Son’
OVERRIDING USING THE KEYWORD ‘SUPER’
Example:
class W3TrainingSchool {
public void course( ) {
System.out.println(“‘W3TS offers Digital Marketing Course’”) ;
}
}
class W3TS extends W3TrainingSchool {
public void course( ) {
super.course( ) ;
System.out.println(“‘W3TS offers Web Designing Course’”) ;
}
}
class Overriding {
public static void main(String args[]) {
W3TrainingSchool obj = new W3TS( ) ;
obj.course( ) ;
}
}
Output:
‘W3TS offers Digital Marketing Course’
‘W3TS offers Web Designing Course’
Also Read : String Classes in Java Programming
We have provided you the best possible description on Method Overriding in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.