Latest web development tutorials

Java example - cover method

Java Examples Java Examples

Previous chapters we have learned Java method of rewriting this article we look at the implementation of the Java method coverage.

Method overloading and Method overriding differences are as follows:

  • Method overloading (Overloading): If there are two methods the same method name, but the parameters are inconsistent, it can be said that a method which is overloaded with another method.
  • The method of covering (Overriding): If you define a method in a subclass, its name, return type and parameter signature coincided with the name of a parent class method, the return type and parameter signature match, then it can be said, the subclass method covering the parent class.

The following example demonstrates Java methods to achieve coverage (Overriding) code:

/*
 author by w3cschool.cc
 Findareas.java
 */
public class Findareas{
   public static void main (String []agrs){
      Figure f= new Figure(10 , 10);
      Rectangle r= new Rectangle(9 , 5);
      Figure figref;
      figref=f;
      System.out.println("Area is :"+figref.area());
      figref=r;
      System.out.println("Area is :"+figref.area());
   }
}
class Figure{
   double dim1;
   double dim2;
   Figure(double a , double b) {
      dim1=a;
      dim2=b;
   }
   Double area() {
      System.out.println("Inside area for figure.");
      return(dim1*dim2);
   }
}
class Rectangle extends Figure {
   Rectangle(double a, double b) {
      super(a ,b);
   }
   Double area() {
      System.out.println("Inside area for rectangle.");
      return(dim1*dim2);
   }
}

The above code is run output is:

Inside area for figure.
Area is :100.0
Inside area for rectangle.
Area is :45.0

Java Examples Java Examples