Play AlgoQuiz

Rate Us :

Share with Friends :


Method Overloading :

Defining more than one method with same name by changing :-

  • number of parameter

  • types of parameter

  • order of parameter

When more than one method having similar operations but different implementation then method is overloaded.

Benefit of Method Overloading :

Simplicity :-

As a developer/programmer doesn't required to remember number of method's name.

Extensibility :-

Adding new features into existing methods without modifying it.

Reuseability :-

Allow to reuse method's name and functionality. Overloaded method having polymorphic behaviour. (Defining one thing in many forms is called "polymorphism". This is called name polymorphism or ad-hoc polymorphism)

Example :

class A { void m1() { } void m1() { } }

Compile time error : method m1() is already defined in class A

Compiler recognize method with its signature, which include number of parameters, types of parameters and order of parameters.

Example 1 :

class A { int m1() { } float m1() { } }

Compile time error : method m1() is already defined in class A

Example 2 :

class A { void m1() { } void m1(int x) { } }

Valid Overloading

Example 1 :

class Employee { private int empno; private String ename; private float Salary; void setAccount(int empno, String ename) { this.empno = empno; this.ename = ename; } void setEmployee(int empno, String ename, float Salary) { this.empno = empno; this.ename = ename; this.Salary = Salary; } String getEmployee() { return empno + " " + Salary; } } class Payroll { public static void main(String [ ] args) { Employee emp1 = new Employee(); Employee emp2 = new Employee(); emp1.setEmployee(101, "John", 5000f); emp2.setEmployee(102, "Jack", 6000f); String e1 = emp1.getEmployee(); String e2 = emp2.getEmployee(); System.out.println(e1); System.out.println(e2); } }

Output :

// save as : Payroll.java // compile as : javac Payroll.java // run as : java Payroll 101 5000.0 102 6000.0

Example 2 :

What will be the output ? class Algo { void mini(byte b) { System.out.print("Algo"); } void mini(int i) { System.out.print("Valley"); } } class Valley { public static void main(String args[ ]) { Algo obj = new Algo(); obj.mini(127); } }

Output :

// save as : Valley.java // compile as : javac Valley.java // run as : java Valley Valley Explanation : By default, in Java any whole number [in the range of integer i.e. -231 to 231-1] taken as integer

The behaviour (selection of method) is done during compile time. So, this is called compile time polymorphism.

Compile time Polymorphism :

Method overloading exhibit compile time polymorphism. As selection of method is done during compile time based on number of parameters, types of parameters or order of parameters. The method which is selected by compiler, the same method is executed by JVM during runtime. As behaviour of this method is changed at compile time, it is called compile time polymorphism.

Example 1 :

class Account { private int accno; private String name; private float balance; void setAccount(int accno , String name) { this.accno = accno; this.name = name; } void setAccount(int accno , String name , float balance) { setAccount(accno , name); this.balance = balance; } String getAccount() { return accno + " " + name + " " + balance; } } class Bank { public static void main(String [ ] args) { Account acc1 = new Account(); Account acc2 = new Account(); acc1.setAccount(101 , "John"); acc2.setAccount(102 , "Jack" , 5000f); String a1 = acc1.getAccount(); String a2 = acc2.getAccount(); System.out.println(a1); System.out.println(a2); } }

Output :

//save as : Bank.java //compile as :javac Bank.java //run as : java Bank 101 John 0.0 102 Jack 5000.0

static modifier :

static is a modifier applicable for variables and methods but not for classes (but inner classes can be declared as static). If the value of a variable is varied from object to object then we should go for instance variable. In the case of instance variable, for every object a separate copy will be created. If the value of variable is same for all objects then we should use static variables. In such case only one copy will be created at class level.

static variable :

static variables are class variables for which memory is allocated on loading of class. Java doesn't support global variables. In order to declare global variable, which is global to more than one object, it is declared with static modifier.

Syntax : static data_type variable_name;

The static variable is loaded in class context area. Static variables are shared by more than one object. Static variables bind with class name or object name.

Example :

class Account { int accno; String name; static float rate; } Account acc1 = new Account(); Account acc2 = new Account();

In order to declare a variable which is common for more than one object declared as static.

static method :

The methods defined within class can be static. Static methods are class methods. These methods are bind with class name or object name. It is a global method, which is global to more than one class or object. The operation or method which doesn't involved on creation of object, that operation or method declare as static.

Syntax : static return_type method_name(parameter) { statements; }

Example 1 :

class A { int x = 10; static void print() { System.out.print(x); } }

The above program will displays an compile time error because static method cannot access non - static variable.

Example 2 :

class A { int x = 10; public static void main(String [ ] args) { System.out.print(x); } }

The above program will displays an compile time error because main() is a static method which cannot access 'x'. For invoking non - static members we must create object.

Example 3 :

class Math { static int sqr(int n) { return n*n; } static int max(int x, int y) { if (x > y) return x; else return y; } } class Calc { public static void main(String [ ] args) { int s = Math.sqr(5); int m = Math.max(20,10); System.out.println("Sqr is " + s); System.out.println("Max is " + m); } }

Output :

// save as Calc.java // compile as : javac Calc.java // run as : java Calc Sqr is 25 Max is 20

static block :

A block with static keyword is called static block. One class can have any number of static blocks, and all those blocks are executed from top to bottom order, whenever class is loaded. static blocks are executed before main() method execution.

Example :

class Block { static { System.out.println("static block1"); } public static void main(String [ ] args) { System.out.println("main method"); } static { System.out.println("static block2"); } }

Output :

// save as Block.java // compile as : javac Block.java // run as : java Block static block1 static block2 main method


MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.