7-Access Specifiers, Packages
7-Access Specifiers, Packages
7-Access Specifiers, Packages
Method Overriding
Access Specifiers
Packages
Method Overriding
If subclass has the same method as declared in parent class, it is method overriding.
Usage:
•to provide specific implementation of a method that is already provided by its super class.
•used for runtime polymorphism
class Test2{
public static void main(String args[]) {
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getROI());
System.out.println("ICICI Rate of Interest: "+i.getROI());
System.out.println("AXIS Rate of Interest: "+a.getROI());
}}
this keyword
6 usage of java this keyword:
class Animal{
String color="white";
}
class Dog extends Animal{ String color="black"; Output:
void printColor(){ System.out.println(color);//prints color of Dog class black
System.out.println(super.color);//prints color of Animal class white
}
}
class TestSuper1{
public static void main(String args[]){ Dog d=new Dog();
d.printColor();
}}
Final Keyword
Once any entity (variable, method or class) is declared final, it can be assigned only once.
That is,
class FinalVar {
• created a final variable named age.
public static void main(String[] args) {
• tried to change the value of the final variable.
• On execution, a compilation error with the
// create a final variable
final int AGE = 32; following message appeared,
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a
way to destroy the unused objects.
• It makes java memory efficient because garbage collector removes the unreferenced objects
from heap memory.
• It is automatically done by the garbage collector(a part of JVM) so we don't need to make
extra efforts.
gc() method
used to invoke the garbage collector to perform cleanup processing.
The gc() is found in System and Runtime classes.
• There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and
support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for GUI (like button , menus etc).
6) java.net: Contain classes for supporting networking operations.
How to Create a package?
• Choose the name of the package. Include the package command as the first line of code in Source File.
• The Source file contains the classes, interfaces, etc you want to include in the package. Compile to create
the Java packages
A folder with the given package name is created in the specified destination, & the compiled class files will be placed in
that folder.
The -d switch specifies the destination where to put the generated class file. You can use any directory name like
d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
Additional points about package:
• Package statement must be first statement in the program even before the import statement.
• A package is always defined as a separate folder having the same name as the package name.
• Store all the classes in that package folder.
• All classes of the package which we wish to access outside the package must be declared public.
• All classes within the package must have the package statement as its first line.
• All classes of the package must be compiled before use.
package mypack;
How to compile java package public class Simple
{
syntax :
public static void main(String args[])
javac -d directory javafilename { System.out.println("Welcome to package");
}
How to run java package program }
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
user-defined packages:
Examples: • created a class Calculator inside a package name letmecalculate.
• declare the package name in the first statement of program.
• A class can have only one package declaration.
• Calculator.java file created inside a package letmecalculate
package letmecalculate;
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
There are three ways to access the package from outside the package :
a)import package.*;
b)import package.classname;
c)fully qualified name.
1) Using packagename.* : all classes and interfaces of this package are accessible but not subpackages.
The import k/w is used to make classes & interface of another package accessible to current package.
//save by A.java
package pack; Example:
public class A {
public void msg() {
Output: Hello! I
System.out.println("Hello! I am from class A, package pack"); } }
am from class A,
//save by B.java package pack
package mypack;
import pack.*;
class B {
public static void main(String args[]) {
A obj = new A();
obj.msg();
} }
2) Using packagename.classname: only declared class of this package will be
accessible.
Example:
//save by A.java
package pack;
public class A {
public void msg() {
System.out.println("Hello ! I am from class A, package pack"); } }
Output: Hello! I
//save by B.java am from class A,
package mypack; package pack
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
} }
3) Using fully qualified name
Only declared class of this package will be accessible. Now, import is not required.
Needed to use fully qualified name every time the class or interface are accessed.
- generally used when two packages have same class name e.g. java.util and java.sql
packages contain Date class.
Example: //save by A.java
package pack;
public class A {
public void msg() {
System.out.println(("Hello ! I am from class A, package pack");
} }
//save by B.java
package mypack;
class B {
public static void main(String args[]) {
pack.A obj = new pack.A(); //using fully qualified name
obj.msg();
} }
Subpackage in java
• Package inside the package is called the subpackage.
• Created to categorize the package further.
- On import of a package, all the classes and interface of that package will be imported
excluding the classes and interfaces of the subpackages.
Hence, needed to import the subpackage as well.
For example :
If I create a package inside letmecalculate package then that will be called sub package.
Lets say I have created another package inside letmecalculate and the sub package name is multiply.
So if I create a class in this subpackage it should have this package declaration in the beginning:
package letmecalculate.multiply;
// Save as Multiplication.java
letmecalculate.multiply.Multiplication obj =
new letmecalculate.multiply.Multiplication();
Points to remember:
1. Sometimes class name conflict may occur. For example: Lets say we have two
packages abcpackage and xyzpackage and both the packages have a class with the
same name, let it be JavaExample.java. Now suppose a class import both these
packages like this:
import abcpackage.*;
import xyzpackage.*;
This will throw compilation error. To avoid such errors you need to use the fully
qualified name method that I have shown above. For example
package abcpackage;
import xyzpackage.*;
3. A class can have only one package declaration but it can have more than one package
import statements. For example:
So , on importing the package abc using wildcard like this: import abc.*;
will only import classes Example1, Example2 and Example3 but it not import the classes of sub package.
So to import all the classes present in package and subpackage, use two import statements like this:
import abc.*;
import abc.todo.*;
Access Control
Access Modifiers in java
There are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers: specifies accessibility (scope) of a data member, method,
constructor or class.
1. private
2. default
3. protected
4. public
(a) private access modifier
Its accessible only within class.
Example: created two classes A and Simple. A class contains private data member and
private method. We are accessing these private members from outside the class, so
there is compile time error.
class A{
private int data=40;
private void msg()
{System.out.println("Hello java");} }
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");} } As the scope of class A and its method msg() is
default so it cannot be accessed from outside
//save by B.java the package.
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A(); //Compile Time Error
obj.msg(); //Compile Time Error } }
(c) protected access modifier
Its accessible within package and outside the package but through inheritance only.
Can be applied on the data member, method and constructor. It can't be applied on the class.
Example: we have created the two packages pack and mypack. The A class of pack package is public, so can be
accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed
from outside the class only through inheritance.
//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(); Output:Hello
obj.msg();
}}
(d) public access modifier
Its accessible everywhere. It has the widest scope among all other modifiers.
Example:
//save by A.java
package pack;
public class A{
public void msg()
{System.out.println("Hello");} }
//save by B.java Output:Hello
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}}
Access Modifiers with Method Overriding
If you are overriding any method, overridden method (i.e. declared in subclass) must not be
more restrictive.
class A{
protected void msg(){System.out.println("Hello java");} }
The default modifier is more restrictive than protected. Thus, there is a compile-time error.
the access modifiers by a simple table: