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
- class circle
- {
- double r;
-
- circle()
- {
- r = 10;
- }
-
- void getarea()
- {
- double a;
- a = (3.14 * r *r);
- System.out.println("\nArea of Circle is : " + a);
- }
- }
-
- class cylinder extends circle
- {
- double r,h;
-
- cylinder(double rad, double height)
- {
- r = rad;
- h = height;
- }
-
- void getarea()
- {
- double a;
- a = (3.14*r*r*h);
- System.out.println("\nArea of Cylinder is : " + a);
- }
- }
-
- class area
- {
- public static void main(String arg[])
- {
- circle c = new circle();
- cylinder l = new cylinder(5,10);
- circle re;
-
- re = c;
- re.getarea();
-
- re = l;
- re.getarea();
- }
- }
Explanation
In this blog, I explained about Method Overriding concept in Java programming. The output will be displayed in the Run module.
Output