codingstreets
Search
Close this search box.
java-access-specifier

Get Started: Java Access Specifier

In This Article, You Will Learn to know about Java Access Specifier.

Java Access Specifier – Before moving ahead, let’s know a bit about Data Types in Java.

Table of Contents

Access Specifier

Access Specifier – It is a restriction line in Java that allows to access of class object, variable etc., but with some restriction.

E.g., A protected class instances can be accessed within same class but not from different package non-subclass.   

E.g., Admission is open for all but who have got 95% + will be rewarded with 10% scholarship in admission fee.

Types of Access Specifier 

Public specifier

Public specifier allows to accessible of instances (method, object, variable) publically by all classes.  

Private specifier

Private specifier allows to accessible of instances from only to its own class.

Protected specifier

Protected specifier allows to accessible of instances from the same package or by subclasses in different package.

Default specifier

Default specifier allows to accessible of instances from only to its own package.

Methods

Method

The method is collection of instructions or block of code that can do a specific task. 

Method Signature

It is a part of the method declaration. It includes the method name and parameter list.

Method Body

It is a part of the method declaration which contains all the tasks to be performed. It is enclosed within the pair of curly braces.

Return Type

Every method returns some value which is known as Return type. It may return a primitive data type, object etc. If the method does not return anything, we use void keyword.

Naming a Method

The method name must start with a lowercase letter. In the multi-word method name, the first letter of each word must be in uppercase except the first word.

Example:

Single-word method name: sum(), add(), area() etc.

Multi-word method name: areaOfTriangle(), perimeterOfCircle, stringAdd() etc.

Types of Method

There are two types of methods in Java. Predefined Method and User-defined Method.

Pre-defined

Pre-defined is built-in/already defined method. Ex: equals(), length(), sqrt(), etc. Java doesn’t allow any modification in it.

E.g., Find the square-root of 25.

				
					public class Main{
    public static void main(String[] args){
        System.out.println(Math.sqrt(25));
    }
}
				
			

In this example, three built-in methods namely main(), print(), and sqrt() have been used. You can see these methods have been used directly without declaration because they are predefined. The sqrt() method is a method of the Math class which returns the square root of a number.

User-defined

User-defined is type of method that is written by user, it can be modified as per need. Ex: myName(), totalNum(), etc.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on