Java method overloading and  overriding method

Java method overloading and overriding method

Java overloading and method overriding method

Method overloading in Java

Method overloading: When a method in a class of Java having the same method name with different arguments or signatures is called method overloading.Java method overloading is also known as Static Polymorphism. Method overloading enhances readability.

Why method overloading?

Suppose we need to perform a multiplication function, and the user can input one or two or three or more arguments, and it is also impossible to declare, assign or define new methods for each and every number of user inputs as it will be confusing and time-consuming. Here we can use method overloading for this problem.

public class OverLoadFunction
    int a, b;
    char a1, b1;
    String a2, b2;
    void compare(int x, int y)
        a= x;
        b= y;
    void compare(char x, char y)
        a1= x;
        b1= y;
         System.out.println("Higher value="+a1);
         System.out.println("Higher Value="+b1);
    void compare(String x, String y)
        a2= x;
        b2= y;
        int n, m;
        n= a2.length();
        m= b2.length();
         System.out.println("Longer String="+a2);
         System.out.println("Longer String="+b2);
    public static void main(String args[])throws IOException
        InputStreamReader in=new InputStreamReader(;
        BufferedReader br=new BufferedReader(in);
        int a, b;        // Local Variables
        char a1, b1;
        String a2, b2;
        OverLoadFunction ob=new OverLoadFunction();
        System.out.println("Enter two number");
        a= Integer.parseInt(br.readLine());
        b= Integer.parseInt(br.readLine());,b);
        System.out.println("Enter two Character");
        a1= (char);
        b1= (char);,b1);
        System.out.println("Enter two String");
        a2= br.readLine();
        b2= br.readLine();,b2);

Method overriding in Java

Method overriding: Overridden the function is the same name method both in the Base class and in the Derive class. The same name function and the same signature of the base class are overwritten in the drive class. It is must be an IS-A relation. It is used for Runtime polymorphism. A static method can not override in Java. 
//First Class
public class ClassA
   void display()
       System.out.println("I am from ClassA");
//Second class
public class ClassB extends ClassA
    void display()
       System.out.println("I am from ClassB");
   public static void main(String args[])
       ClassB ob=new ClassB();

More Java program
Method Overloading
Important interview QA

Important interview QA

Q. What is the difference between an argument and a parameter?
While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

Q. What are different types of access modifiers?
 Public: Anything declared as public can be accessed from anywhere.
Private: Anything declared as private can’t be seen outside of its class.
Protected: Anything declared as protected can be accessed by classes in the same package and subclasses in the other packages.
Default modifier: Can be accessed only to classes in the same package.

Q. What is final, finalize() and finally?
final: The final keyword is used to declare constants or final variables, final classes and final methods. The final variable cannot be assigned a value once they are initialized. The final calss cannot be extended and cannot be override the final method.

finalize( ) : finalize( ) method is used just before an object is destroyed and can be called just prior to
garbage collection.
finally : finally, a keyword used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. 
Q. What is UNICODE?
Unicode is used for internal representation of characters and strings and it uses 16 bits to represent each other.

Q. What is Garbage Collection and how to call it explicitly?
Ans: When an object is no longer referred to by any variable, java automatically reclaims memory used by that
object. This is known as garbage collection.
System.gc() method may be used to call it explicitly.

Q. What are Transient and Volatile Modifiers?
Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.
Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
 Q. What is method overloading and method overriding?
Method overloading: When a method in a class having the same method name with different arguments or signature is said to be method overloading.

Method overriding : Overridden function is the same name method both in the Base class and in the Derive class. The same name function and same signature of base class is over write in the drive class.
Q. What is Recursion
Recursion is function which call itself. This enables the function to repeat itself several times, outputting the result and the end of each iteration.
Java program for finding largest number

Java program for finding largest number

// Maximum number finding
Java Program For Finding Largest Number

In this program you can find or search maximum number from an array. He we define an array of five elements and and then search the maximum number. Then display the number.

 *  class ArrayMax here.
 * @author (Khurshid Md Anwar)
public class ArrayMax
    public static void main(String args[])throws IOException
            InputStreamReader ab=new InputStreamReader(;
            BufferedReader br=new BufferedReader(ab);
            int a[]=new int[5];  // Creating array
            int i,max;
            // Inserting Array elements
            System.out.println("Enter Five elements:");

Enter Five elements:

 Java program for Recursion Fibonacci Series

Java program for Recursion Fibonacci Series

Java program for Recursion Fibonacci Series

Recursion Fibonacci Series 

Fibonacci series or numbers denoted by Fn. It is a series of numbers formed by the addition of the preceding two numbers in the series. The first two terms are 0 and 1 respectively. The terms after this are generated by simply adding the previous two terms.
For example of Fibonacci series: 0,1,1,2,3,5,8,13….etc.
* Recursion Fibonacci Series 
class RecFibonacci
    static BufferedReader br=new BufferedReader(new InputStreamReader(;
    int a,b,c,l;
RecFibonacci() //Constructor
void input()throws IOException //Function to input the limit
     System.out.print("Enter the limit : ");
int fib(int n) //
    return a;
    else if(n==2)
    return b;
    return (fib(n-1)+fib(n-2));
void fiboSeries() //Function generating all the Fibonacci Series numbers upto 'n' terms
        System.out.println("The Fibonacci Series is:");
        for(int i=1;i<=l;i++)
            System.out.print(c+"  ");
public static void main()throws IOException
   RecFibonacci ob=new RecFibonacci();

More Program

Recursion program in Java

Java program for Stack

Java Interview QA

Java Interview QA

What is significance of import* in your program?
A: The line imports all the classes of the package into the current program.
Name the primitive data-types in java.
A: byte, short, int, long, float, double, char and Boolean
Give some examples of packages.
A: java.util, java.lang etc.
What is a class?
A: A class is the blueprint from which individual objects are created.
What is an object?
A: An object is an instance of a class.
Why do you write BufferedReader br = new ……. ?
A: To activate the Buffer memory for efficient input and output operations. ‘br’ is an object of the BufferedReader class.
Why do we have main() function?
A: The execution of the program begins from the main() method.
Why is the main method public?
A: So that it be accessible to the JVM which begins to execute the program from outside of the class.
Why is the main method static?
A: So that it be available for execution without the need of any object.
Is it compulsory to write String args[]?
A: No it is not.
Why do you write ‘throws IOException’?
A: For handling any input/output exceptions.
What are exceptions?
A: Exceptions are runtime errors which prevent the program from working normally.
Mention the two types of exceptions?
A: Checked Exceptions – Exceptions which are checked (handled) during compile time.
Example: IOException.
Unchecked Exceptions – Exceptions which are not checked during compile time.
Example: ArrayIndexOutOfBound.
Mention other ways in which java handles exceptions.
A: Java can handle exception using the try-catch block, throws keyword and throw keyword.
What is the difference between throws and throw?
A: Using throws keyword, we can give system defined error message if any error occurs, while using throw keyword, we can force an exception and give user-defined error messages.
What are wrapper class?
A wrapper class is a class which wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used.
What is type conversion? Name its types.
A: Converting a calue of a particular data type into another data-type is called type conversion. It is of two types:

(a) Implicit Type Conversion: When the conversion takes place on its own without the programmer’s intervention.
(b) Explicit Type Conversion: When the conversion takes place with the programmer’s intervention.
What are comments? Name the different types.
A: Comments are statements which enhances the readability and understanding of the program. They are not part of the program.

The different types are: single line (//….), multiple line (/* … */) and documenting comment (/**….*/).
What is Bubble sort?
A: Bubble sort is one of the easiest method of Sorting . This method based on successively selecting the smallest element, second smallest, third smallest and so on. The larger element push to down and do the sorting in ascending order.
Java Interview QA

Java Interview QA

Q.What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces. Conceptually we can think of packages as being similar to different folders on our computer. We might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Q. Mention any two attributes required for class declaration.
Ans: Variables and function or state and behaviour.
Q. Differentiate between base and derived class.
The base class is a class whose properties will be inherited and the derived class is a class which inherits the base class. E.g. Of base class is Super and derived class is sub.


Q. Define Constructor. When a constructor is called?
Ans: A constructor is special member function that is automatically called when an instance of a class is declared.
A constructor is called when an object of the class is created.
Q. Why do we need a constructor as a class member?
Ans: Constructors have one purpose in life: to create an instance of a class.
Q. What is a default constructor? Why is a default constructor called so?
Ans: A default constructor is a special member function that initializes the objects.
It is called because it declares no parameters. It gets invoked without any parameter
Q. What are the characteristics of constructor function?
Ans: Constructors are the special member functions and have the same name as class and it is called automatically when object being create.
Q. What is parameterisedconstructor?
Ans: The constructors which can take different parameters are referred to as Parameterised Constructors.
Q. What is constructor overloading?.

Ans: Defining more than one constructor for a class where they differ in the parameter types or number of parameters passed to the constructor is called overloading.