• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Tuesday, April 19, 2016


     What is Method overloading?

    Answer:

    Method overloading is also a capability of object oriented programming language. If same method will be used more than one time with different types of argument is called Method overloading. In this program, compiler select the argument number and its type automatically and print the value for the specific argument.

    In method overloading, only one method and two or more argument will use.
    In method overloading there is no effect of return type.
    The following program “Method” is a function member or method which is used three times with three types of argument. And compiler select the appropriate task or arguments for this method. And so it is called Method overloading.

    package Method_Overloading;
    import java.util.Scanner;
    public class Overloading {
          Scanner input= new Scanner(System.in);
          int Method(int number){
          System.out.println("The area of circle is : "+ (3.1416*number * number));
          return 0;
          };
          double Method(double length, double width){
          System.out.printf("The area of rectangle is : %.2f\n", length*width);
          return 0;
          }
          int Method(double base, int height ){
          System.out.println("This is triangle area and is first parameter double and second "
                + "is integer type\nArea of triangle is : "+0.5*base*height);
          return 0;
          }
    }

    Now from the main class. First create an object of Overloading class named over. then use the method overloading formula :

    package Method_Overloading;
    public class MainClass {
    public static void main(String[] args) {
          Overloading over =new Overloading();
          over.Method(20);
          over.Method(45.40, 10.05);
          over.Method(45.40, 20);
          }

    }

     What is method overriding?

    Answer:

    When user inherits any method from the base class to derived class which prototype, return type and name is same but derived class explain another different method, then derived class method is executed and for that base class is overridden.
    By this system, without using base classes method, another derived class method using is called method overriding.
    For more clear method overriding, you need to clear your idea on super-class and sub-class.



    Suppose in any class we've declared a method called sup()


    1. String NameClass(int id){
    2.      Scanner sc = new Scanner(System.in);
    3.      id = sc.nextInt();
    4.      String name="Maniruzzaman Akash";

    5.      return name;
    6. }



    Now if you want to use this class again you can do this :


    1. String detailClass extends NameClass(){
    2.       int reg = 05385;
    3.       float cgpa = 3.0;

    4.      //now you can get the NameClass's all property's here:

    5.     System.out.println("Your total info is : \n");
    6.     System.out.println("Name is : "+name+"\nId is : "+ id+"\nRegistration no is :"+reg +""+ "\nCGPA is:"+cgpa +"\n");
    7. }






    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel