Method Overloading
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
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
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