Java Modifiers Homework Help
Java Modifiers:
The java also has the feature like other language is we can modify the methods, classes using modifiers. There are two types of modifiers are available. It is used to restrict the access of data member and method in another class. Two types of access modifiers are available in java.
1. Access modifiers – restrict the access level
2. Non – Access modifiers – do not restrict but provides other fuctionality
Four types of access modifiers are available.
- Default
- Public
- Private
- protected
Default Modifiers:
The default modifiers mean we need not to specify any modifiers. It is supported only limited package only. That is if we declare class with default modifiers with imitated package. We can access only that package inside class other package not supported.
Private Modifiers:
It restricts the access with in the class only. So the private data members and methods accessed with in class. We cannot declare the class as private because if it have private constructor we cannot create an object from outside of the class.
Ex:
The following example shows compilation errors because we try to access private data member and method from XYZ class, outside of the class.
class XYZ {
private double n1 = 25;
private int square(int x)
{
return x*x;
}
}
public class sample {
public static void main(String args[]) {
XYZ obj1 = new XYZ();
System.out.println(obj1.n1);
System.out.println(obj1.square(5));
}
}
Output:
Compile time error
Public Modifiers:
The public modifier of data members and methods can be access anywhere from the program. There is no any restriction.
Example:
Now we consider following example with public modifiers that can access from other package also.
package one;
public class Add
{
public int addNumbers(int x, int y)
{
return x+y;
}
}
sample.java
package two;
import one.*;
class sample
{
public static void main(String args[])
{
Add obj1 = new Add();
System.out.println(obj1.addNumbers(50, 1));
}
}
Output:
51
Protected Modifier:
The protected modifier of data members and methods can be accessed with in class of the same package and sub class also. It same like that default modifier but it is visible in sub class also.
Example:
The following example have class sample it is available in another package. It is have rights to access twonumbers() that is declared as protected modifiers. Addition.java
package one;
public class Add
{
protected int addNumbers(int x, int y)
{
return x+y;
}
}
sample.java
package two;
import one.*;
class sample extends Add
{
public static void main(String args[]) {
sample obj1 = new sample();
System.out.println(obj1.addNumbers(10, 12));
}
}
Output:
22
Java Tutorials
- Environment setup
- Basic concept
- Object Classes
- Constructors
- OOPS in java
- data abstraction
- Variable Types
- Modifiers
- Operators
- Loop Controls
- Decision Making
- Strings
- Arrays
- Date and Time
- Methods in java
Java sample assignments
Programming Topics
- Ada
- Assembly Language
- AutoCAD
- BASIC
- Computer virus
- C Programming
- Euphoria
- Fortran Homework Help
- Game programming language
- Java Assignment Help
- JavaScript
- Java Servlets Help
- Machine Language
- Matlab
- Pascal
- Perl
- PHP
- Python
- Ruby
- Servlet Life Cycle
- Smalltalk
- SOAP
- Visual Basic
- COBOL
- Lisp
- Logo Help
- Plankalkul Help
- Prolog
- REBOL
- Rexx
- Scheme Help
- TCL
- ToonTalk Help