Module 4 Java
Module 4 Java
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.
Declaration
Interfaces are declared by specifying a keyword “interface”.
interface interfacename
{
variable declaration;
Methods declaration;
}
Relationship between classes and interfaces.
Output:
implementation of method1
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
}
interface Myinterface2
{
public void method2();
}
}
}
Output:
implementation of method1
implementation of method2
Extending interfaces:
interface Myinterface1
{
public void method1();
}
interface Myinterface2
{
public void method2();
}
interface Myinterface3 extends Myinterface1, Myinterface2
{
public void method3();
}
Output:
implementation of method1
implementation of method2
implementation of method3
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.
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
interface interface_name{
...
interface nested_interface_name{
...
}
}
Example:
interface Showable{
void show();
interface Message{
void msg();
TestNestedInterface1 message=new
TestNestedInterface1(); /*[Link] message=new TestNestedInterface1();*/
[Link]();
}
}
Output:
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.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
//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.
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 Protection()
{
[Link]("base constructor");
[Link]("n = " + n);
[Link]("n_priv = " + n_priv);
[Link]("n_prot = " + n_prot);
[Link]("n_publ = " + n_publ);
}
}
package pkg1;
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); */
package pkg2;
class OtherPackage
{
OtherPackage()
{
[Link] pro = new [Link]();
/* class only
* [Link]("n_priv = " + pro.n_priv); */
package pkg1;
package pkg2;
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]();
}
}
}