Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Protected Modifier
Previous Home Next

The protected modifier makes a class member more visible than private modifier but less accessible than public modifier. This modifier may be applied only to class members viz. variables, methods, and inner classes—but not to the class itself. A class member declared protected is visible to the following elements:

  1. The classes in the same package that contains the class that owns the protected member.

  2. The subclasses of the class that owns the protected member. These subclasses have access even if they are not in the same package as the parent class.

The usage of protected modifier can be shown as,

Example


Java Tutorialslass ModifierDemo
 {
 private int i=10;
 protected int j=5;
 }
Java Tutorialslass PublicModifier extends ModifierDemo
{
	public static void main(String[] args) 
	{
		PublicModifier obj=new PublicModifier();
	    System.out.println("The value of i is :"+obj.i);   
		// cause a compile error as i's defined private.
		System.out.println("The value of j is :"+obj.j);   
		// print the value of j =5, as j is declared protected
	}
}

default modifier

In java we cannot specify any modifier for the variables inside a method, and also we cannot specify the protected modifier for a class. When we do not specify any access modifier for an element, it is implied that the access is default. It can be applied to a class, a variable, or a method.

A class or a class member declared default i.e. with no access modifier specified is accessible from anywhere in the same package in which the accessed class exists. Consider the following code fragment:


package example;
Java Tutorialslass ShowExample
 {
private int i=10;
 void Show()
{
 System.out.println("The value of  i is :"+i);
}
}

Above code contains a method of default modifier show returning void, and it is contained in a package called example.


package example2;
Java Tutorialslass ReviewExample extends ShowExample
{
Show();     // will cause compiler error
}

The Show() method called in class ReviewExample causes a compiler error, as Show() method is declared default in ShowExample which is in a totally different package and, it is obvious that a default method cannot be accessed from a different package other than in which it is defined.

Previous Home Next