Module-4
INTERFACES
Interface
Interface looks like class but it is not a class. Using interface we can fully abstract a class
interface from its implementation. An interface can have methods and variables just like the
class but the methods declared in interface are by default abstract (only method signature, no
body). Also, the variables declared in an interface are public, static & final by default. Once it
is defined a number of classes can implement an interface .Also, one class can implement any
number of interfaces.
To implement a interface a class must create complete set of methods defined by the
interface.
What is the use of interfaces?
As mentioned above they are used for abstraction. Also, java programming language does not
support multiple inheritances, using interfaces we can achieve this as a class can implement
more than one interfaces, however it cannot extend more than one classes.
Declaration
Interfaces are declared by specifying a keyword “interface”.
interface interfacename
{
variable declaration;
Methods declaration;
}
Relationship between classes and interfaces.
Creating and implementing a interface:
Example:
interface MyInterface
{
public void method1();
}
class interfacedemo implements MyInterface
{
public void method1()
{
[Link]("implementation of method1");
}
public static void main(String arg[])
{
Interfacedemo obj = new interfacedemo();
obj. method1();
}
}
Output:
implementation of method1
using interface reference:
interface MyInterface
{
public void method1();
}
class interfacedemo implements MyInterface
{
public void method1()
{
[Link]("implementation of method1");
}
public static void main(String arg[])
{
MyInterface obj = new interfacedemo();
obj. method1();
}
}
Output:
implementation of method1
Multiple inheritance in Java by interface:
Example1:
interface Myinterface1
{
public void method1();
}
interface Myinterface2
{
public void method2();
}
class interfacedemonotp implements Myinterface1,Myinterface2
{
public void method1()
{
[Link]("implementation of method1");
}
public void method2()
{
[Link]("implementation of method2");
}
public static void main(String arg[])
{
interfacedemonotp obj = new interfacedemonotp();
obj. method1();
obj. method2();
}
}
Output:
implementation of method1
implementation of method2
Extending interfaces:
Example 2: Interface can extend many interfaces separated by commas.
interface Myinterface1
{
public void method1();
}
interface Myinterface2
{
public void method2();
}
interface Myinterface3 extends Myinterface1, Myinterface2
{
public void method3();
}
class interfacedemo3 implements Myinterface3
{
public void method1()
{
[Link]("implementation of method1");
}
public void method2()
{
[Link]("implementation of method2");
}
public void method3()
{
[Link]("implementation of method3");
}
public static void main(String arg[])
{
Interfacedemo3 obj = new interfacedemo3();
obj. method1();
obj. method2();
obj. method3();
}
}
Output:
implementation of method1
implementation of method2
implementation of method3
Java Nested Interface
An interface, i.e., declared within another interface or class, is known as a nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to maintain.
The nested interface must be referred to by the outer interface or class. It can't be accessed
directly.
Points to remember for nested interfaces
There are given some points that should be remembered by the java programmer.
o The 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
Syntax of nested interface which is declared within the interface
interface interface_name{
...
interface nested_interface_name{
...
}
}
Example:
interface Showable{
void show();
interface Message{
void msg();
class TestNestedInterface1 implements [Link]{
public void msg(){[Link]("Hello nested interface");}
public static void main(String args[]){
TestNestedInterface1 message=new
TestNestedInterface1(); /*[Link] message=new TestNestedInterface1();*/
[Link]();
}
}
Output:
Hello nested interface
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.
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Simple example of java package
The package keyword is used to create a package in java.
//save as [Link]
package mypack;
public class Simple{
public static void main(String args[]){
[Link]("Welcome to package");
}
}
To Compile: javac -d . [Link]
To Run: java [Link]
Output:Welcome to package
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?
There are three ways to access the package from outside the package.
1. import package.*;
2. import [Link];
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 of package that import the packagename.*
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
[Link]();
}
}
Output: Hello
2) Using [Link]
If you import [Link] then only declared class of this package will be accessible.
Example of package by import [Link]
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
[Link]();
}
}
Output: Hello
3) Using fully qualified name
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. [Link] and [Link]
packages contain Date class.
Example of package by import fully qualified name
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
[Link]();
}
}
Output: Hello
Access modifiers:
Access modifiers define the scope of the class and its members (data and methods). For
example, private members are accessible within the same class members (methods). Java
provides many levels of security that provides the visibility of members (variables and
methods) within the classes, subclasses, and packages. Packages are meant for
encapsulating, it works as containers for classes and other subpackages. Class acts as
containers for data and methods. There are four categories, provided by Java regarding
the visibility of the class members between classes and packages:
● Subclasses in the same package
● Non-subclasses in the same package
● Subclasses in different packages
● Classes that are neither in the same package nor subclasses
Example:
package pkg1;
public class Protection
{
int n = 1;
private int n_priv = 2;
protected int n_prot = 3;
public int n_publ = 4;
public Protection()
{
[Link]("base constructor");
[Link]("n = " + n);
[Link]("n_priv = " + n_priv);
[Link]("n_prot = " + n_prot);
[Link]("n_publ = " + n_publ);
}
}
This is [Link] file:
package pkg1;
class Derived extends Protection
{
Derived()
{
[Link]("derived constructor");
[Link]("n = " + n);
[Link]("n_prot = " + n_prot);
[Link]("n_publ = " +n_publ);
}
}
This is [Link] file:
package pkg1;
class SamePackage
{
SamePackage()
{
Protection pro = new Protection();
[Link]("same package constructor");
[Link]("n = " + pro.n);
/* class only
* [Link]("n_priv = " + pro.n_priv); */
[Link]("n_prot = " + pro.n_prot);
[Link]("n_publ = " + pro.n_publ);
}
}
package pkg2;
class Protection2 extends [Link]
{
Protection2()
{
[Link]("derived other package constructor");
/* class or package only
* [Link]("n = " + n); */
/* class only
* [Link]("n_priv = " + n_priv); */
[Link]("n_prot = " + n_prot);
[Link]("n_publ = " + n_publ);
}
}
This is [Link] file:
package pkg2;
class OtherPackage
{
OtherPackage()
{
[Link] pro = new [Link]();
[Link]("other package constructor");
/* class or package only
* [Link]("n = " + pro.n); */
/* class only
* [Link]("n_priv = " + pro.n_priv); */
/* class, subclass or package only
* [Link]("n_prot = " + pro.n_prot); */
[Link]("n_publ = " + pro.n_publ);
}
}
The test file for pkg1
/* demo package pkg1 */
package pkg1;
/* instantiate the various classes in pkg1 */
public class Demo
{
public static void main(String args[])
{
Protection obj1 = new Protection();
Derived obj2 = new Derived();
SamePackage obj3 = new SamePackage();
}
}
The test file for pkg2 is shown below :
/* demo package pkg2 */
package pkg2;
/* instantiate the various classes in pkg2 */
public class Demo
{
public static void main(String args[])
{
Protection2 obj1 = new Protection2();
OtherPackage obj2 = new OtherPackage();
}
}
Static Import:
Used to import static members (variables or methods) directly so you can use them without class
name.
import static [Link].*;
public class StaticImportDemo {
public static void main(String[] args) {
[Link](sqrt(16)); // no need to write [Link]()
[Link](PI); // directly access PI
}
}
FILE I/O (Input/Output):
Java provides classes in the [Link] package for file operations.
Create a File:
import [Link].*;
class CreateFile {
public static void main(String[] args) {
try {
File file = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
} else {
[Link]("File already exists.");
}
} catch (IOException e) {
[Link]();
}
}
}
Write to a File:
import [Link];
import [Link];
class WriteFile {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("[Link]")) {
[Link]("Hello, File Handling in Java!");
[Link]("Successfully written to file.");
} catch (IOException e) {
[Link]();
}
}
}
Read from a File
import [Link];
import [Link];
class ReadFile {
public static void main(String[] args) {
try (FileReader fr = new FileReader("[Link]")) {
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}
} catch (IOException e) {
[Link]();
}
}
}