access modifiers in java :: futureX

 There are 4 types of access modifiers:


  1. private
  2. default
  3. protected
  4. public



Private :
The private access modifier is accessible only within class.


example :
class Emp{
private int id;
private void show(){System.out.println(id);}
}

class Test{
public static void main(String args[]){
Emp e=new Emp();
e.id=10;
e.show();
}
}

output:compile time error


Note: A class cannot be private or protected except nested class.


Default :
If you don't use any modifier, it is treated as default modifier by default. The default modifier is accessible only within package.

example:

//save as A.java
package com.futurex;
public class A{
void msg(){System.out.println("hello");}
}

//save as Test.java
package com.techshakti;
import com.futurex.*;
class Test{
public static void main(String args[]){
A a=new A();
a.msg();
}
}

output: msg() is not public in A; cannot be accessed from outside package


Protected :
The protected access modifier is accessible within package and outside the package by only through inheritance.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.

example:


//save as A.java
package com.futurex;
public class A{
protected void msg(){System.out.println("hello");}
}

//save as Test.java
package com.techshakti;
import com.futurex.*;
class Test extends A{
public static void main(String args[]){
Test t=new Test();
t.msg();
}
}



Public:
The public access modifier is accessible everywhere. It has the widest scope among all other modiers.

example:

//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}


//save as B.java
package mypack;
import pack.*;

class B{
  public static void main(String args[]){
   A obj = new A();
   obj.msg();
  }
}


                  JAVA TUTORIALS HOMEPAGE

basic HTML :: futureX

HTML Headings


HTML headings are defined with the <h1> to <h6> tags.

Example

<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
html heading example :: futureX

HTML Paragraphs

HTML paragraphs are defined with the <p> tag.

Example

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

HTML Links

HTML links are defined with the <a> tag.

Example

<a href="http://www.futurextech.blogspot.com">This is a link</a>



Note: The link address is specified in the href attribute.


HTML Images

HTML images are defined with the <img> tag.

Example

<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXGB1U7-60jnOvecQdP_Wqk5F3kJ5OOZOwAvC0zv-vkpHpIRIUBK68VzbvpdECUHAqRJz-84xpM9hupSCiT-GrD6zpDZVn6_UozjhTF6TBKhTshuAj8TPm9RLnug7nT5szSb3p8O3WMLfY/s1600/images.jpg" width="104" height="142">


Note: The filename and the size of the image are provided as attributes.




« Previous Chapter                                                                                                                                         Next Chapter »


A package is a group of similar types of class, interfaces and sub-packages.
Package can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java.lang, awt, javax, swing, net, io, util, sql etc.
package in java :: futureX

In this page, we will have the detailed learning of creating user-defined packages.

Advantage of Package

  • Package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Package providEs access protection.
  • Package removes naming collision.

Example

To compile : javac -d . Simple.java
To run       : java mypack.Simple

package com;
class Simple{
public static void main(){
System.out.println("hello java");
}
}

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.


How to access package from another package?

  • import package.*;
  • import package.classname;
  • fully qualified name.



The import keyword is used to make the classes and interface of another package accessible to the current package.



If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.

If you import package.classname then only declared class of this package will be accessible but not subpackages.

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

package math;
public class Calc{
public static int cube(int n){return n*n*n;}
}


//import all the classes of that package

package com;
import math.*;
class Simple{
public static void main(String args[]){
System.out.println(Calc.cube(5));
}
}

//only declared class is acessible

package com;
import math.Calc;
class Simple{
public static void main(String args[]){
System.out.println(Calc.cube(5));
}
}




//without import, full qualified name

package com;
class Simple{
public static void main(String args[]){
System.out.println(math.Calc.cube(5));
}
}


Subpackages:

Package inside the package is called the subpackage. It should be created to categorize the package further. Let's take an example, Sun Microsystems has defined a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket classes in net packages and so on.

standard declaration:
com.companyname.packagename.classname

package com.techshakti.math;
public class subpackage{
public static int cube(int n){return n*n*n;}
}


                                          JAVA TUTORIALS HOMEPAGE
interface in java :: futureX

Interface: An interface is a blueprint of a class. It has static constants and abstract methods.
The interface is a mechanism to achieve abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java.

Why use Interface?

It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling
the java compiler converts methods of interface as public and abstract, data members as public,final and static by default.

Example :

interface Shape{
void draw();
}

class Circle implements Shape{
public void draw(){System.out.println("drawing circle");}
}

class Rectangle implements Shape{
public void draw(){System.out.println("drawing rectangle");}
}

class Test{
public static void main(String args[]){
Shape S=new Circle();
S.draw();

}}

multiple inheritance by interface :

interface Shape{
void draw();
}

interface Color{
String getColor();
}


class Rectangle implements Shape,Color{
public void draw(){System.out.println("drawing rectangle");}
public String getColor(){return("red");}
}

class Test{
public static void main(String args[]){
Rectangle s=new Rectangle();
s.draw();
System.out.println(s.getColor());
}}

                                            JAVA TUTORIALS HOMEPAGE

abstraction in java :: futureX

Abstraction is a process of hiding the implementation details and showing only functionality to the user.Abstraction lets you focus on what the object does instead of how it does it.


Ways to achieve Abstraction:
Abstract class (0 to 100%)
Interface (100%)

Abstract class:
A class that is declared as abstract is known as abstract class.It needs to be extended and its method implemented.It cannot be instantiated.


           abstract class class_name{}

Abstract method:
A method that is declared as abstract and does not have implementation is known as abstract method.


          abstract return_type method_name();    //no braces{}

Example:

abstract class Shape{
abstract void draw();
}

class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}

class Circle extends Shape{
void draw(){System.out.println("drawing circle");}
}

class Test{
public static void main(String args[]){
Shape s=new Circle();
s.draw();
}
}


Note: An abstract class can have data member,abstract method,method body,constructor and even main() method.

Rule: If there is any abstract method in a class, that class must be abstract.

//Shape is not abstract and does not override abstract method //draw() in shape

class Shape{
abstract void draw();
}


                   JAVA TUTORIALS HOMEPAGE




Copyright © 2013 futureX | Blogger Template by Clairvo