Java Access Specifiers
Access specifier defines the boundary and scope to access the method, variable,
and class etc. Java has defines four type of access specifiers such as:-
1. public
2. private
3. protected
4. default / friendly
These access specifiers are defines the scope for variables/methods. If you are
not defining any access specifier so it will be as 'default' access specifier for
variables/methods.
1. public access specifier:- If you declare any variable/ function as public
access specifier so you can used it from anywhere of the program.
Syntax:-
< Access Specifier > data type variable /function ;
Ex:- public int number;
2. private access specifier:- If you declare any variable/ function as private
access specifier so you can used within the java class itself, not from outside
the class, package and others.
Syntax:-
< Access Specifier > data type variable / function;
Ex:- private int number=10;
3. protected access specifier:- If you want to access the private variable of
parent class within your child class so you can declare those variable as
protected. If your variable/method is declared a as protected so that
variables/methods can be access same Class name, Package name, and sub
class.
Syntax:-
< Access Specifier > data type variable / function;
Ex:- protected int number=10;
4. default access specifier:- Actually, there is no default access modifier; the
absence of a modifier is treated as default. A method or variable, declared
default( that is, no access modifier specified at all ), can be accessed by any
class belonging to the same package.
Classes belonging to other packages cannot access. That is why default access
modifier is known as package level access.
A class can be either default or public.
Note: default is keyword and is used with switch statements.
Constructor Overloading in Java
In Java, a constructor is just like a method but without return type. It can also
be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one
constructor with different parameter lists. They are arranged in a way that each
constructor performs a different task. They are differentiated by the compiler by
the number of parameters in the list and their types.
Example of Constructor Overloading
1. //Java program to overload constructors
2. class Student5{
3. int id;
4. String name;
5. int age;
6. //creating two arg constructor
7. Student5(int i,String n){
8. id = i;
9. name = n;
10. }
11. //creating three arg constructor
12. Student5(int i,String n,int a){
13. id = i;
14. name = n;
15. age=a;
16. }
17. void display(){[Link](id+" "+name+" "+age);}
18.
19. public static void main(String args[]){
20. Student5 s1 = new Student5(111,"Karan");
21. Student5 s2 = new Student5(222,"Aryan",25);
22. [Link]();
23. [Link]();
24. }
25. }
Output:
111 Karan 0
222 Aryan 25
Java Method Overloading :
Method Overloading is a feature that allows a class to have more than one
method having the same name, if their argument lists are different. It is similar
to constructor overloading in Java, that allows a class to have more than one
constructor having different argument lists.
Three ways to overload a method
In order to overload a method, the argument lists of the methods must differ in
either of these:
1. Number of parameters.
For example:
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
EXAMPLE -1
class DisplayOverloading
{
public void disp(char c)
{
[Link](c);
}
public void disp(char c, int num)
{
[Link](c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
[Link]('a');
[Link]('a',10);
}
}
EXAMPLE -2
class DisplayOverloading2
{
public void disp(char c)
{
[Link](c);
}
public void disp(int c)
{
[Link](c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
[Link]('a');
[Link](5);
}
}
EXAMPLE -3
class DisplayOverloading3
{
public void disp(char c, int num)
{
[Link]("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
[Link]("I’m the second definition of method disp"
);
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
[Link]('x', 51 );
[Link](52, 'y');
}
}