JAVA UNIT-II (Part-1)
JAVA UNIT-II (Part-1)
UNIT - II
PACKAGES
PACKAGES
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.
Example:
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.printl
n("Welcome to
package");
6. }
7. }
CLASSPATH
run java package program:
Syntax : Package <Packagename> // at the start of the program.
In java, the access modifiers define the accessibility of the class and its members.
For example, private members are accessible within the same class members only.
Java has four access modifiers, and they are default, private, protected, and public.
In java, the package is a container of classes, sub-classes, interfaces, and sub-
packages. The class acts as a container of data and methods. So, the access
modifier decides the accessibility of class members across the different packages.
In java, the accessibility of the members of a class or interface depends on its
access specifiers. The following table provides information about the visibility of
both data members and methods.
🔔 The protected members are accessible to every child class (same package or
other packages).
🔔 The default members are accessible within the same package but not outside the
package.
Example:
class ParentClass{
int a = 10;
public int b = 20;
protected int c = 30;
private int d = 40;
void showData() {
System.out.println("Inside ParentClass");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
class ChildClass extends ParentClass{
void accessData() {
System.out.println("Inside ChildClass");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
//System.out.println("d = " + d);// private member can't be
accessed
}
}
public class AccessModifiersExample {
IMPORTING PACKAGES
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
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.
Example:
1. //save by A.java
2. package pack;
3. public class A{
4. public void
msg(){System.out.pri
ntln("Hello");} 5. }
1. //save by B.java
2. import pack.*;
3. class B{
4. public static void main(String args[]){
5. A obj = new A();
6. obj.msg();
7. } }
2) Using packagename.classname
1. //save by B.java
2. import pack.A;
3. class B{
4. public static void main(String args[]){
5. A obj = new A();
6. obj.msg();
7. } }
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.
Example:
1. //save by A.java
2. package pack;
3. public class A{
4. public void
msg(){System.out.pri
ntln("Hello");} 5. }
1. //save by B.java
2. class B{
3. public static void main(String args[]){
4. pack.A obj = new pack.A();//using fully qualified name
5. obj.msg();
6. } }
INTERFACES
In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body. It cannot be instantiated just like
the abstract class.
Syntax:
1. interface <interface_name>{
2. // declare constant fields
3. // declare methods that abstract
4. // default. 5. }
Note: The Java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data members.
In other words, Interface fields are public, static and final by default, and the
methods are public and abstract.
Example:
1. interface printable{
2. void print(); 3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6. public static void main(String args[]){
7. A6 obj = new A6();
8. obj.print();
9. }}
Example:
1. //Interface declaration: by first user
2. interface Drawable{
3. void draw(); 4. }
5. //Implementation: by second user
6. class Rectangle implements Drawable{
7. public void draw(){System.out.println("drawing rectangle");
8. }
9. }
9. class Circle implements Drawable{
10. public void draw(){System.out.println("drawing circle");}
11. }
12. //Using interface: by third user
13. class TestInterface1{
14. public static void main(String args[]){
15. Drawable d=new Circle();//
16. d.draw(); 17. }}
implement any number of interfaces. When a class wants to implement more than
one interface, we use the implements keyword is followed by a comma-separated
list of the interfaces implemented by the class.
The following is the syntax for defineing a class that implements an interface.
Syntax
interface it must provide the body of the method those defined in the printable
interface.
Example:
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10. public static void main(String args[]){
11. A7 obj = new A7();
12. obj.print();
13. obj.show();
14. }}
Example:
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void print();
6. }
7. class TestInterface3 implements Printable, Showable{
8. public void print(){System.out.println("Hello");}
9. public static void main(String args[]){
10. TestInterface3 obj = new TestInterface3();
11. obj.print();
12. }}
Interface inheritance
A class implements an interface, but one interface extends another interface.
Example:
1. interface Printable{
2. void print();
3. }
accessed directly.
Points to remember:
o Nested interface must be public if it is declared inside the interface but it can
have any access
modifier if declared within the class.
o Nested interfaces are declared static implicitely.
5. } }
6. class TestNestedInterface1 implements Showable.Message{
7. public void msg(){System.out.println("Hello nested interface");}
8. public static void main(String args[]){
9. Showable.Message obj=new TestNestedInterface1();//upcasting here
10.obj.msg();
11. } }
As you can see in the above example, we are accessing the Message interface by
its outer interface
Showable because it cannot be accessed directly. In collection frameword, sun
microsystem has provided a nested interface Entry. Entry is the subinterface of
Map i.e. accessed by Map.Entry.
Internal code generated by the java compiler for nested interface Message
The java compiler internally creates public and static interface as displayed below:
1. public static interface Showable$Message
2. {
3. public abstract void msg();
4. }
4. } }
5. class TestNestedInterface2 implements A.Message{
6. public void msg(){System.out.println("Hello nested interface");}
7. public static void main(String args[]){
8. A.Message obj=new TestNestedInterface2();//upcasting here
9. obj.msg();
10. } }
If we define a class inside the interface, java compiler creates a static nested class.
Let's see how can
we define a class within the interface:
1. interface M{
2. class A{}
3. }
INTERFACE VARIABLES
A Java interface can contain both variables and constants. However, often it does
not makes sense to place variables in an interface. In some cases it can make sense
to define constants in an interface.
Especially if those constants are to be used by the classes implementing the
interface, e.g. in calculations, or as parameters to some of the methods in the
interface. However, my advice to you is to avoid placing variables in Java
interfaces if you can.
All variables in an interface are public, even if you leave out the public keyword in
the variable declaration
EXTENDING INTERFACES
An interface can extend another interface in the same way that a class can extend
another class.
The extends keyword is used to extend an interface, and the child interface inherits
the methods of
the parent interface.
The following Sports interface is extended by Hockey and Football interfaces.
// Filename: Sports.java
public interface Sports {
public void setHomeTeam(String name);
// Filename: Football.java
public interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
// Filename: Hockey.java
public interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods. Abstract class and interface both can't be
instantiated.
But there are many differences between abstract class and interface that are given
below.
Abstract Interface
class
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables.
variables.
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
Let's see a simple example where we are using interface and abstract class both.
1. //Creating interface that has 4 methods
2. interface A{
3. void a();//bydefault, public and abstract
4. void b();
5. void c();
6. void d();
7. }
8. //Creating abstract class that provides the implementation of one method of A
interface
9. abstract class B implements A{
10. public void c(){System.out.println("I am C");}
11. }
12. //Creating subclass of abstract class, now we need to provide the
implementation of rest of the methods
13. class M extends B{
14. public void a(){System.out.println("I am a");}
15. public void b(){System.out.println("I am b");}
16. public void d(){System.out.println("I am d");}
17. }
18. //Creating a test class that calls the methods of A interface
19. class Test5{
20. public static void main(String args[]){
21. A a=new M();
22. a.a();
23. a.b();
24. a.c();
25. a.d();
26. }
27. }