IMPORTANT CONCEPT OF JAVA PART-2
Importants Points
-------------------
Encapsulation
------------------
The process of binding the data and code as a single unit is called encapsulation.
Or
The process of hiding the implementation details to user is called encapsulation
How to achieve
------------------
we are achieving this concept by declaring variables as a private modifier because it is possible to access private members with in the class only.
1.If the variables are declared as a private it is possible to access those variables only with in the class but it possible to set(update) the data by using setter methods and it is possible to get(read) the data by using getter methods.
2.The setter method return type is always void & getter method return type is always property return type
Packages----------
Types of packages:-
There are two types of packages in java
1) Predefined packages.
2) User defined packages.
Predefined packages:
----------------------------
The predefined packages are introduced by James Gosling and these packages contains predefined classes & interfaces and these class & interfaces contains predefined variables and methods.
Example:- java.lang, java.io ,java.util…..etc
User defined packages:-
-----------------------------
1.The packages which are defined by user, and these packages contains user defined classes and interfaces.
2.Declare the package by using package keyword.
syntax : package package-name;
eg. package com.digicoders;
compilation
----------------------
If the source file contains the package statement then compile by using this command
javac -d . Filename.java
-d ---- Creates folder structure
. ----Place the directory structure in current working folder
execution process
---------------------
java packagename.classname;
Points
---------
1.If it is a predefined package or user defined package Whenever we are using other package classes then must import that package by using import statement.
2.Whenever we are using other package classes then that classes must be public otherwise compiler generate error message.
Public modifier:-
---------------------
1. Public modifier is applicable for variables,methods,classes.
2. All packages are able to access public members.
Default modifier:-
-------------------
1.It is applicable for variables,methods,classes.
2. We are able to access default members only within the package and it is not possible to access outside package .
3. Default access is also known as package level access.
4. The default modifier in java is default.
Note : When we declare class as public the corresponding members are not public, if we want access public class members that members also must be public.
Private modifier:-
--------------------
1.private modifier applicable for methods and variables.
2. We are able to access private members only within the class and it is not possible to access even in child classes.
Note :- the most accessable modifier in java Is public & most restricted modifier in java is private.
Protected modifier:-
-------------------------
1.Protected modifier is applicable for variables,methods.
2.We are able access protected members with in the package and it is possible to access outside packages also but only indirect child classes & it is not possible to call even in indirect child classes.
3. But in outside package we can access protected members only by using child reference. If we try to use parent reference we will get compile time error.
Applicable modifiers on constructors:-
---------------------------------------------
1) Public
2) Private
3) Protected
4) default
we are achieving singleton class creation by using private constructors in java:-
1. when we declare constructor with private modifier we can’t create object outside of the class.
2. Singleton class allows to create only one object of particular class and we are achieving singleton class creation by using private constructors.
Post a Comment