Saltar al contenido principal
Java/J2EE Programming Training
OOPs with Java Contd.
Page 2Classification: Restricted
Agenda
• Packaging Classes & Access Modifiers
Java Package
Classification: Restricted Page 8
• A java package is a group of similar types of classes, interfaces and sub-
packages.
• Package in java 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.
• Advantages
oJava package is used to categorize the classes and interfaces so that
they can be easily maintained.
oJava package provides access protection.
oJava package removes naming collision
Java Package
Classification: Restricted Page 9
Java Package Example
Classification: Restricted Page 10
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Accessing package
Classification: Restricted Page 11
There are three ways to access the package from outside the package.
• import package.*;
• import package.classname;
• fully qualified name.
Accessing package: Using packagename.*
Classification: Restricted Page 12
• If you use package.* then all the classes and interfaces of this package will
be accessible but not subpackages.
• The import keyword is used to make the classes and interface of another package
accessible to the current package.
//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();
}
}
Accessing package: Using packagename.classname
Classification: Restricted Page 13
• If you import package.classname then only declared class of this package
will be accessible.
//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save as B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){ A
obj = new A();
obj.msg();
}
}
• 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.
• It is generally used when two packages have same class name e.g. java.util and java.sql packages
contain Date class.
//save by A.java package
pack; public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java package
mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualifiedname
obj.msg();
}
}
Classification: Restricted Page 14
Accessing package: Using fully qualified name
Packages.. rules
• If you import a package, subpackages will not be imported.
• If you import a package, all the classes and interface of that package
will be imported excluding the classes and interfaces of the
subpackages. Hence, you need to import the subpackage as well.
• Java program structure
• The standard of defining package is domain.company.package e.g.
com.mindsmapped.bean or org.wikipedia.dao.
Classification: Restricted Page 15
More rules…
Classification: Restricted Page 16
• There can be only one public class in a java source file and it must
be saved by the public class name.
//save as C.java otherwise Compile Time Error
class A{}
class B{}
public class C{}
How to put 2 public classes in a package?
Classification: Restricted Page 17
If you want to put two public classes in a package, have two java source files
containing one public class, but keep the package name same.
//save as A.java
package dummy;
public class A{}
//save as B.java
package dummy;
public class B{}
Static import
Classification: Restricted Page 18
• The import allows the java programmer to access classes of a package
without package qualification whereas the static import feature allows
to access the static members of a class without the class qualification
import static java.lang.System.*;
class StaticImportExample{
public static void main(String args[]){
out.println("Hello"); //Now no need of System.out
out.println("Java");
}
}
Access Modifiers
Classification: Restricted Page 19
Private access modifier
Classification: Restricted Page 20
• The private access modifier is accessible only within class.
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
Private constructor example
Classification: Restricted Page 21
class A{
private A(){}//private constructor
void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();//Compile Time Error
}
}
Can a class be private?
Classification: Restricted Page 22
Rule:A class cannot be private or protected except nested class.
Default access modifier
Classification: Restricted Page 23
If you don't use any modifier, it is treated as default bydefault. The default
modifier is accessible only within package.
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A(); //Compile Time
Error obj.msg(); //Compile Time
Error
}
}
protected access modifier
Classification: Restricted Page 24
• The protected access modifier is accessible within package and outside
the package but through inheritance only.
• The protected access modifier can be applied on the data member, method and
constructor. It can't be applied on the class.
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
public access modifier
Classification: Restricted Page 25
• The public access modifier is accessible everywhere. It has the widest scope
among all other modifiers.
//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();
}
}
Overriding rule…
Classification: Restricted Page 26
• If you are overriding any method, overriding method (i.e. declared in
subclass) must not be more restrictive.
class A{
protected void msg(){System.out.println("Hello java");}
}
public class Simple extends A{
void msg(){System.out.println("Hello java");}//Compile-
time error public static void main(String args[]){
Simple obj=new
Simple(); obj.msg();
}
}
Encapsulation in Java
Classification: Restricted Page 27
• Encapsulation in java is a process of wrapping code and data together
into a single unit, for example capsule i.e. mixed of several medicines.
• The Java Bean class is the example of fully encapsulated class.
• Advantages:
• By providing only setter or getter method, you can make the
class read-only or write-only.
• It provides you the control over the data. Suppose you want to set the
value of id i.e. greater than 100 only, you can write the logic inside the
setter method.
Java Bean Example (Best Practice)
Classification: Restricted Page 28
//save as Student.java
package com.dummy;
public class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
Thank You