ENCAPSULATION IN JAVA
It is the process of data together into a single unit and wrapping code.
Example of the encapsulation in java is mixed of several medicines.
By making all the data members of the class private we can create a fully encapsulated class in java.
Now to set and get the data in it we can use setter and getter methods.
Advantages of encapsulation in java:
To make the class read only or write only, setter or getter method is provided.
It also control over the data.
Access Modifiers in Java
Modifiers contains two types
1. Access Modifiers
2. Non Access Modifiers
The Access Modifiers specifies accessibility of a method, data method, class or constructor.
Access Modifiers in Java contains 4 types
Static, abstract, synchronized, native, volatile, transient etc are non access modifiers.
Private Access Modifier:
The Private Access Modifier is the Modifier that is accessible only within the class.
Default Access Modifier:
By default, it is treated as default when any modifier is not in use. Within package only default modifier is accessible.
Protected Access Modifier:
Through inheritance the Protected Access Modifier is accessible within the package and outside the package.
The Protected Access Modifier can’t be applied on the class. It can be applied only on the constructor, method and data member.
Public Access Modifier:
The Public Access Modifier has the widest scope among all the modifiers. It can be accessible everywhere.
Java Access Modifiers with method Overriding:
Over ridden method must not be more restructive when if you are overriding any method.
A Java Package is a group which contains similar types of sub packages, interface and classes.
Built in Package and User–defined Package are the two forms which can be categorized by package in Java.
Advantage of Java Package:
Classes and Interfaces are easily maintained as it is categorized by a Java Package.
It removes naming collision and provides access protection removes naming collision.
How to Access Package from another Package:
1. Using packagename.*
All the classes and interface will be accessible for packages but not for sub packages by using Using packagename.*
2. Using packagename.classname
Declared class of this package will only be accessible If you import package.classname.
Visit Best Coding Classes in vizag
3. Using fully qualified name
The declared class of this package will be accessible If you use fully qualified name only. Now import is not needed. But when you are accessing the class or interface you need to use fully qualified name every time. It is used when any two packages have the same class name
Sub Package in Java:
The package inside the package is said as Subpackage. To categorize the package further the subpackage should be created.
System, String, Reader, Writer, Socket etc are the classes of java package which is defined by the Sun Microsystem. In which there classes represents a particular group.
Eg :- For Input/ Output , Reader and Writer are the two classes. For networking, Socket and Server socket are the two classes etc and so on. Lang, Net, IO etc are the sub packages of Java Package which is sub categorized by sun, and put the Input/ Output related classes in IO package and Net package which contains server and server socket classes and so on.
How to put two Public Classes in a Package:
To, put two public classes in a package. Keep the package name same for two java source file containing one public class.
Visit for Best Java Training Institute in Vizag.