Java - Method Overriding

Introduction

 
In this blog, I will explain about the Method Overriding concept in Java. It is very simple in Java programming. The output will be displayed in the Run module.
 

Software Requirement

 
JDK1.3.
  

Overriding

 
The benefit of overriding is the ability to define a behavior, which is specific to the subclass type, which means a subclass can implement a parent class method, based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method.

Simple program
  1. class circle {  
  2.  double r;  
  3.  circle() {  
  4.   r = 10;  
  5.  }  
  6.  void getarea() {  
  7.   double a;  
  8.   a = (3.14 * r * r);  
  9.   System.out.println("\nArea of Circle is : " + a);  
  10.  }  
  11. }  
  12. class cylinder extends circle {  
  13.  double r, h;  
  14.  cylinder(double rad, double height) {  
  15.   r = rad;  
  16.   h = height;  
  17.  }  
  18.  void getarea() {  
  19.   double a;  
  20.   a = (3.14 * r * r * h);  
  21.   System.out.println("\nArea of Cylinder is : " + a);  
  22.  }  
  23. }  
  24. class area {  
  25.  public static void main(String arg[]) {  
  26.   circle c = new circle();  
  27.   cylinder l = new cylinder(510);  
  28.   circle re;  
  29.   re = c;  
  30.   re.getarea();  
  31.   re = l;  
  32.   re.getarea();  
  33.  }  
  34. }   
Explanation
 
In this blog, I explained about Method Overriding concept in Java programming. The output will be displayed in the Run module.

Output 
 
 
m