Access Modifiers in Java:
❑ There are two types of modifiers in Java: access modifiers and non-access
modifiers.
❑ The access modifiers in Java specifies the accessibility or scope of a field,
method, constructor, or class.
❑ We can change the access level of fields, constructors, methods, and class
by applying the access modifier on it.
There are four types of Java access modifiers:
Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
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.
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.
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.
Private:-
The private access modifier is accessible only within the class.
Ex:-
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
Default:-
▪ If you don't use any modifier, it is treated as default by default.
▪ The default modifier is accessible only within package.
▪ It cannot be accessed from outside the package.
▪ It provides more accessibility than private. But, it is more restrictive than
protected, and public.
Ex:-//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
In the above example, the scope of class A and its method msg() is default
so it cannot be accessed from outside the package.
Protected:-
❑ The protected access modifier is accessible within package and outside the
package but through inheritance only.
❑ If a member declared as protected then we can access that member within
the current package anywhere but outside package only in child class.
❑ We can access protected members within the current package either by
using parent or child reference.
❑ But we can access protected members from outside package only through
child reference.
❑ The protected access modifier can be applied on the data member, method
and constructor. It can't be applied on the class.
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Public:-
The public access modifier is accessible everywhere. It has the widest scope
among all other modifiers.
Example of public access modifier
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
/save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
o/p:-hello
Methods in java:-
▪ Method in Java is a collection of instructions that performs a specific
task.
▪ Main advantage of method is code optimization and code reusability.
▪ A method is a block of code or collection of statements or a set of code
grouped together to perform a certain task or operation.
Method Declaration
Method Signature: Every method has a method signature. It is a part of the
method declaration. It includes the method name and parameter list.
Access Specifier: public, private, protected and default
Return Type: Return type is a data type that the method returns.
If the method does not return anything, we use void keyword.
Method Name: It is a unique name that is used to define the name of a
method.
Parameter List: It is the list of parameters separated by a comma and
enclosed in the pair of parentheses. It contains the data type and variable
name. If the method has no parameter, left the parentheses blank.
Naming a Method:-
Single-word method name: sum(), area()
Multi-word method name: areaOfCircle(), stringComparision()
Types of Method:-
There are two types of methods in Java:
Predefined Method
User-defined Method
Predefined Method:-
predefined methods are the method that is already defined in the Java
class libraries is known as predefined methods.
It is also known as the standard library method or built-in method.
We can directly use these methods just by calling them in the program at
any point. Some pre-defined methods are length(),
equals(),compareTo()etc.
User-defined Method:-
The method written by the user or programmer is known as a user-defined
method.
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from the user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
}
We have defined the above method named findevenodd(). It has a
parameter num of type int. The method does not return any value that's
why we have used void. The method body contains the steps to check the
number is even or odd. If the number is even, it prints the number is
even, else prints the number is odd.
Addition.java
public class Addition
{
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
int c = add(a, b); //a and b are actual parameters
System.out.println("The sum of a and b is= " + c);
}
//user defined method
public static int add(int n1, int n2) //n1 and n2 are formal parameters
{
int s;
s=n1+n2;
return s; //returning the sum
Static Method:-
▪ A method that has static keyword is known as static method.
▪ We can also create a static method by using the keyword static before the
method name.
▪ The main advantage of a static method is that we can call it without creating
an object.
▪ It is invoked by using the class name.
▪ The best example of a static method is the main() method.
Display.java
public class Display
{
public static void main(String[] args)
{
show();
}
static void show()
{
System.out.println("It is an example of static method.");
}
}
o/p:-It is an example of a static method.
Instance Method:-
The method of the class is known as an instance method.
It is a non-static method defined in the class.
Before calling or invoking the instance method, it is necessary to create an
object of its class.
InstanceMethodExample.java
public class InstanceMethodExample
{
public static void main(String [] args)
{
//Creating an object of the class
InstanceMethodExample obj = new InstanceMethodExample();
//invoking instance method
System.out.println("The sum is: "+obj.add(12, 13));
int s;
//user-defined method because we have not used static keyword
public int add(int a, int b)
{
s = a+b;
//returning the sum
return s;
}
}
Abstract Method:
The method that does not has method body is known as abstract method.
It always declares in the abstract class.
abstract class Demo //abstract class
{
//abstract method declaration
abstract void display();
}
public class MyClass extends Demo
{
//method impelmentation
void display()
{
System.out.println("Abstract method?");
}
public static void main(String args[])
{
//creating object of abstract class
Demo obj = new MyClass();
//invoking abstract method
obj.display();
}
}