0% found this document useful (0 votes)
10 views

Method Overloading

Method overloading in java
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Method Overloading

Method overloading in java
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Method Overloading

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
Advantage of method overloading
Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
By changing number of arguments
By changing the data type

1) Method Overloading: changing no. of arguments


In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs
addition of three numbers.
class Adder{  
static int add(int a,int b){return a+b;}  
static int add(int a,int b,int c){return a+b+c;}  
}  
class TestOverloading1{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(11,11,11));  
}} 
 2) Method Overloading: changing data type of arguments
class Adder{  
static int add(int a, int b){return a+b;}  
static double add(double a, double b){return a+b;}  
}  
class TestOverloading2{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(12.3,12.6));  
}}  
 Why Method Overloading is not possible by changing the return type of method only?
 Can we overload java main() method?
Final Keyword In Java

 The final keyword in java is used to restrict the user. 


 he java final keyword can be used in many context. Final can be:
 Variable -If you make any variable as final, you cannot change the value of final variable(It will be constant).
 Method -If you make any method as final, you cannot override it.
 Class -If you make any class as final, you cannot extend it.
 Java Garbage Collection
 In java, garbage means unreferenced objects.
 Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a
way to destroy the unused objects.
 Advantage of Garbage Collection
 It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
 It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
 How can an object be unreferenced?
 There are many ways:
 By nulling the reference
Employee e=new Employee();  
e=null;  

 By assigning a reference to another


Employee e1=new Employee();  
Employee e2=new Employee();  
e1=e2;//now the first object referred by e1 is available for garbage collection  

 By anonymous object etc.


new Employee(); 
Java Stack

 The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-
First-Out (LIFO).
  Java collection framework provides many interfaces and classes to store the collection of objects. One of
them is the Stack class that provides different operations such as push, pop, search, etc.
 In Java, Stack is a class that falls under the Collection framework that extends the Vector class. It also
implements interfaces List, Collection, Iterable, Cloneable, Serializable. It represents the LIFO stack of
objects. Before using the Stack class, we must import the java.util package. The stack class arranged in
the Collections framework hierarchy.

 Stack Class Constructor
 The Stack class contains only the default constructor that creates an empty stack.
public Stack()  
 Creating a Stack
If we want to create a stack, first, import the java.util package and create an object of the Stack class.
Stack stk = new Stack();
or
Stack<type> stk = new Stack<>();  
 Where type denotes the type of stack like Integer, String, etc.
 Methods of the Stack Class
Method Modifier and Type Method Description

empty() boolean The method checks the stack is empty or not.

push(E item) E The method pushes (insert) an element onto the top of the
stack.

pop() E The method removes an element from the top of the stack and
returns the same element as the value of that function.

peek() E The method looks at the top element of the stack without
removing it.

search(Object o) int The method searches the specified object and returns the
position of the object.
Access control
 Access control is a mechanism, an attribute of encapsulation which restricts the access
of certain members of a class to specific parts of a program. Access to members of a
class can be controlled using the access modifiers. There are four access modifiers in
Java.
 Public-   The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
 Protected -The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed
from outside the package.
 Default - The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
 Private- The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
 
Access Modifier within class within package outside package by outside package
subclass only

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

You might also like