0% found this document useful (0 votes)
21 views32 pages

Packages (28 03 25)

The document provides an overview of Java packages, including their definition, types (built-in and user-defined), advantages, and how to create and access them. It explains the process of importing packages, the concept of classpath, and the differences between classes and interfaces in Java. Additionally, it covers the implementation of interfaces, including nested interfaces and the use of multiple inheritance through interfaces.

Uploaded by

sarithainti444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views32 pages

Packages (28 03 25)

The document provides an overview of Java packages, including their definition, types (built-in and user-defined), advantages, and how to create and access them. It explains the process of importing packages, the concept of classpath, and the differences between classes and interfaces in Java. Additionally, it covers the implementation of interfaces, including nested interfaces and the use of multiple inheritance through interfaces.

Uploaded by

sarithainti444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Defining, Creating and Accessing a Package, Importing 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.

Here, we will have the detailed learning of creating and using user-defined packages.

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.


A Package can be defined as a grouping of related types (classes, interfaces, enumerations
and annotations) providing access protection and namespace management.

Some of the existing packages in Java are −

 java.lang − bundles the fundamental classes


 java.io − classes for input, output functions are bundled in this package

In java, the packages have divided into two types. 

 Built-in Packages:
The built-in packages are the packages from java API. The Java API is a pre-defined classes,
interfaces, and sub-packages library. The built-in packages were included in the JDK.There
are many built-in packages in java, few of them are as java, lang, io, util, awt, javax, swing,
net, sql, etc. We need to import the built-in packages to use them in our program. To import a
package, we use the import statement. For e.g: we have used java.io package previously
which contain classes to support input / output operations in Java. Similarly, there are other
packages which provides different functionality.

Some of the commonly used built-in packages Description


are shown in the table below : Package Name
java.lang Contains language support classes ( for e.g
classes which defines primitive data types,
math operations, etc.) . This package is
automatically imported.
java.io Contains classes for supporting input / output
operations.
java.util Contains utility classes which implement data
structures like Linked List, Hash Table,
Dictionary, etc and support for Date / Time
operations.
java.applet Contains classes for creating Applets.
java.awt Contains classes for implementing the
components of graphical user interface ( like
buttons, menus, etc. ).
java.net Contains classes for supporting networking
operations.

Accessing classes in a Built-in package in to a program :


1) import java.util.Vector; // import the Vector class from util package

or
2) import java.util.*; // import all the class from util package
First statement imports Vector class from util package which is contained inside java
package.
Second statement imports all the classes from util package.

 User-defined Packages:
The user-defined packages are the packages created by the user. User is free to create
their own packages.

Defining a Package in java


We use the package keyword to create or define a package in java programming language.
Syntax

package packageName;

Simple example of java package

The package keyword is used to create a package in java.

1. //save as Simple1.java
2. package mypack;
3. public class Simple1{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }

How to run java package program

The -d switch specifies the destination where to put the generated class file. You can use any
directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to
keep the package within the same directory, you can use . (dot).

You need to use fully qualified name e.g. mypack.Simple1 etc to run the class.
To Compile: javac -d . Simple1.java

To Run: java mypack.Simple1

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 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 of package that import the packagename.*
1. //save by A.java
2. package pack;
3. public class A{
4. public void msg(){System.out.println("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. }
8. }

To compile: javac -d . B.java


To run: java mypack.B

Output:
Hello

2) Using packagename.classname

In java, the import keyword used to import built-in and user-defined packages. When a
package has imported, we can refer to all the classes of that package using their name
directly.
The import statement must be after the package statement, and before any other statement.
Using an import statement, we may import a specific class or all the classes from a package.
🔔 Using one import statement, we may import only one package or a class.
🔔 Using an import statement, we can not import a class directly, but it must be a part of a
package.
🔔 A program may contain any number of import statements.
Importing specific class
Using an importing statement, we can import a specific class. The following syntax is
employed to import a specific class.
Syntax

import packageName.ClassName;

If you import package.classname then only declared class of this package will be accessible.

Example of package by import package.classname

1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }

1. //save by B.java
2. import pack.A;
3.
4. class B{
5. public static void main(String args[]){
6. A obj = new A();
7. obj.msg();
8. }
9. }

To compile: javac -d . B.java


To run: java mypack.B

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. 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
class B{
public static void main(String args[]){
A obj = new A(); //using fully qualified name
obj.msg();
}
}

To compile: javac -d . B.java


To run: java mypack.B

Output:
Hello

Explicit & Implicit Import


There are two types of import Explicit & Implicit. When we use predefined java classes in our
java code then we need to load that class by the using import keyword at the very first line of
our program.

Explicit import: Classes are available inside the package, Explicit means direct or when we
give the proper path of the java class it will call as explicit import.e.g: import
java.util.ArrayList;

Explicit import

Implicit import : Implicit means indirect, When we load all the classes of the package in our
java code by using (*) it will call as implicit import. E.g: import java.util.*;
Here we have loaded all the classes from util package.

UNDERSTANDING CLASSPATH

What is ClassPath?
From the word, we understand that classpath is a path of a class. A classpath is a path for the
compiler to identify which class, or which package we use in our defined class. It provides
the compiler a way to find classes or packages that are used in our class.
CLASSPATH describes the location where all the required files are available which are
used in the application. Java Compiler and JVM (Java Virtual Machine) use
CLASSPATH to locate the required files. If the CLASSPATH is not set, Java Compiler
will not be able to find the required files and hence will throw the following error.

Error: Could not find or load main class <class name> (e.g. GFG)
The above error is resolved when CLASSPATH is set.

// If the following code is run when the CLASSPATH is not


// set, it will throw the above error.
// If it is set, we get the desired result
import java.io.*;
class Hello {
public static void main(String[] args)
{
System.out.println(Hello World");
}
}

Set the CLASSPATH in JAVA in Windows

Command Prompt:
set PATH=.;C:\Program Files\Java\JDK1.6.20\bin
Note: Semi-colon (;) is used as a separator and dot (.) is the default value
of CLASSPATH in the above command.
1. Select Start

2. Go to the Control Panel

3. Select System and Security

4. Select Advanced System settings

5. Click on Environment Variables


6. Click on New under System Variables

7. Add CLASSPATH as variable name and path of files as a variable value.

1. Select OK.

DIFFERENCE BETWEEN CLASSES AND INTERFACES


Class Interface

The keyword used to create a class is The keyword used to create an interface is
“class” “interface”

A class can be instantiated i.e., objects of An Interface cannot be instantiated i.e.


a class can be created. objects cannot be created.

Classes do not support multiple


The interface supports multiple inheritance.
inheritance.

It can be inherited from another class. It cannot inherit a class.

It can be inherited by a class by using the


It can be inherited by another class using
keyword ‘implements’ and it can be inherited
the keyword ‘extends’.
by an interface using the keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract methods. It contains abstract methods only.

Variables and methods in a class can be


declared using any access All variables and methods in an interface are
specifier(public, private, default, declared as public.
protected).

Variables in a class can be static, final, or


All variables are static and final.
neither.

Defining an interface in java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

How to declare an interface?

An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.

Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}

Implementing Interfaces

 Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword

 To implement an interface, include the implements Keyword in a class definition,


and then create the methods defined by the interface.

 The methods that implement an interface in a class must be declared with public
access Specifier.

Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}

If a class implements more than one interface, the interfaces are separated with a comma.

As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces

Java Interface Example1

In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.

1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }

Output:

Hello

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is


known as multiple inheritance.

Multiple inheritance is not supported through class in java, but it is possible by an


interface, why?

As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.

.Interface inheritance

A class implements an interface, but one interface extends another interface.


1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }

Output:

Hello
Welcome

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{
...
}
}

Syntax of nested interface which is declared within the class


class class_name{
...
interface nested_interface_name{
...
}
}
Example of nested interface which is declared within the interface

In this example, we will learn how to declare the nested interface and how we can access it.

TestNestedInterface1.java

interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}

Output:

hello nested interface

Example of nested interface which is declared within the class

Let's see how we can define an interface inside the class and how we can access it.

TestNestedInterface2.java

1. class A
2. {
3. interface Message
4. {
5. void msg();
6. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
10. public void msg(){System.out.println("Hello nested interface");
11. }
12. public static void main(String args[])
13. {
14. A.Message message=new TestNestedInterface2();//upcasting here
15. message.msg();
16. }
17. }

Output:

hello nested interface

Variables in Java Interfaces

In java, an interface is a completely abstract class. An interface is a container of abstract


methods and static final variables. The interface contains the static final variables. The
variables defined in an interface cannot be modified by the class that implements the
interface, but it may use as it defined in the interface.

🔔 The variable in an interface is public, static, and final by default.


🔔 If any variable in an interface is defined without public, static, and final keywords, then
the compiler automatically adds the same.
🔔 No access modifier is allowed except the public for interface variables.
🔔 Every variable of an interface must be initialized in the interface itself.
🔔 The class that implements an interface cannot modify the interface variable, but it may be
used as it is defined in the interface.

interface SampleInterface{

int UPPER_LIMIT = 100;


}
public class InterfaceVariablesExample implements SampleInterface{

public static void main(String[] args)


{
UPPER_LIMIT = 150; // Can not be modified

System.out.println("UPPER LIMIT = " + UPPER_LIMIT);


}

}
Extending an Interface in java

In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.

A class implements an interface, but one interface extends another interface.

1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }

Output:
DIFFERENCE BETWEEN CLASSES AND INTERFACES

Class Interface

The keyword used to create a class is The keyword used to create an interface is
“class” “interface”

A class can be instantiated i.e., objects of An Interface cannot be instantiated i.e.


a class can be created. objects cannot be created.

Classes do not support multiple


The interface supports multiple inheritance.
inheritance.

It can be inherited from another class. It cannot inherit a class.

It can be inherited by a class by using the


It can be inherited by another class using
keyword ‘implements’ and it can be inherited
the keyword ‘extends’.
by an interface using the keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract methods. It contains abstract methods only.

Variables and methods in a class can be


declared using any access All variables and methods in an interface are
specifier(public, private, default, declared as public.
protected).

Variables in a class can be static, final, or


All variables are static and final.
neither.

Defining an interface in java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

How to declare an interface?

An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.

Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}

Implementing Interfaces

 Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword

 To implement an interface, include the implements Keyword in a class definition,


and then create the methods defined by the interface.

 The methods that implement an interface in a class must be declared with public
access Specifier.

Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}

If a class implements more than one interface, the interfaces are separated with a comma.

As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces

Java Interface Example1

In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.

6. interface printable{
7. void print();
8. }
9. class A6 implements printable{
10. public void print(){System.out.println("Hello");}
6.
12. public static void main(String args[]){
13. A6 obj = new A6();
14. obj.print();
15. }
16. }

Output:

Hello

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is


known as multiple inheritance.

Multiple inheritance is not supported through class in java, but it is possible by an


interface, why?

As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.

.Interface inheritance

A class implements an interface, but one interface extends another interface.


10. interface Printable{
11. void print();
12. }
13. interface Showable extends Printable{
14. void show();
15. }
16. class TestInterface4 implements Showable{
17. public void print(){System.out.println("Hello");}
18. public void show(){System.out.println("Welcome");}
10.
17. public static void main(String args[]){
18. TestInterface4 obj = new TestInterface4();
19. obj.print();
20. obj.show();
21. }
22. }

Output:

Hello
Welcome

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{
...
}
}

Syntax of nested interface which is declared within the class


class class_name{
...
interface nested_interface_name{
...
}
}
Example of nested interface which is declared within the interface

In this example, we will learn how to declare the nested interface and how we can access it.

TestNestedInterface1.java

interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}

Output:

hello nested interface

Example of nested interface which is declared within the class

Let's see how we can define an interface inside the class and how we can access it.

TestNestedInterface2.java

4. class A
5. {
6. interface Message
4. {
7. void msg();
8. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
13. public void msg(){System.out.println("Hello nested interface");
14. }
15. public static void main(String args[])
13. {
18. A.Message message=new TestNestedInterface2();//upcasting here
19. message.msg();
20. }
21. }

Output:

hello nested interface

Variables in Java Interfaces

In java, an interface is a completely abstract class. An interface is a container of abstract


methods and static final variables. The interface contains the static final variables. The
variables defined in an interface cannot be modified by the class that implements the
interface, but it may use as it defined in the interface.

🔔 The variable in an interface is public, static, and final by default.


🔔 If any variable in an interface is defined without public, static, and final keywords, then
the compiler automatically adds the same.
🔔 No access modifier is allowed except the public for interface variables.
🔔 Every variable of an interface must be initialized in the interface itself.
🔔 The class that implements an interface cannot modify the interface variable, but it may be
used as it is defined in the interface.

interface SampleInterface{

int UPPER_LIMIT = 100;


}
public class InterfaceVariablesExample implements SampleInterface{

public static void main(String[] args)


{
UPPER_LIMIT = 150; // Can not be modified

System.out.println("UPPER LIMIT = " + UPPER_LIMIT);


}

}
Extending an Interface in java

In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.

A class implements an interface, but one interface extends another interface.

10. interface Printable{


11. void print();
12. }
13. interface Showable extends Printable{
14. void show();
15. }
16. class TestInterface4 implements Showable{
17. public void print(){System.out.println("Hello");}
18. public void show(){System.out.println("Welcome");}
10.
17. public static void main(String args[]){
18. TestInterface4 obj = new TestInterface4();
19. obj.print();
20. obj.show();
21. }
22. }

Output:
DIFFERENCE BETWEEN CLASSES AND INTERFACES

Class Interface

The keyword used to create a class is The keyword used to create an interface is
“class” “interface”

A class can be instantiated i.e., objects of An Interface cannot be instantiated i.e.


a class can be created. objects cannot be created.

Classes do not support multiple


The interface supports multiple inheritance.
inheritance.

It can be inherited from another class. It cannot inherit a class.

It can be inherited by a class by using the


It can be inherited by another class using
keyword ‘implements’ and it can be inherited
the keyword ‘extends’.
by an interface using the keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract methods. It contains abstract methods only.

Variables and methods in a class can be


declared using any access All variables and methods in an interface are
specifier(public, private, default, declared as public.
protected).

Variables in a class can be static, final, or


All variables are static and final.
neither.

Defining an interface in java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

How to declare an interface?

An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.

Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}

Implementing Interfaces

 Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword

 To implement an interface, include the implements Keyword in a class definition,


and then create the methods defined by the interface.

 The methods that implement an interface in a class must be declared with public
access Specifier.

Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}

If a class implements more than one interface, the interfaces are separated with a comma.

As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces

Java Interface Example1

In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.

11. interface printable{


12. void print();
13. }
14. class A6 implements printable{
15. public void print(){System.out.println("Hello");}
6.
17. public static void main(String args[]){
18. A6 obj = new A6();
19. obj.print();
20. }
21. }

Output:

Hello

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is


known as multiple inheritance.

Multiple inheritance is not supported through class in java, but it is possible by an


interface, why?

As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.

.Interface inheritance

A class implements an interface, but one interface extends another interface.


19. interface Printable{
20. void print();
21. }
22. interface Showable extends Printable{
23. void show();
24. }
25. class TestInterface4 implements Showable{
26. public void print(){System.out.println("Hello");}
27. public void show(){System.out.println("Welcome");}
10.
23. public static void main(String args[]){
24. TestInterface4 obj = new TestInterface4();
25. obj.print();
26. obj.show();
27. }
28. }

Output:

Hello
Welcome

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{
...
}
}

Syntax of nested interface which is declared within the class


class class_name{
...
interface nested_interface_name{
...
}
}
Example of nested interface which is declared within the interface

In this example, we will learn how to declare the nested interface and how we can access it.

TestNestedInterface1.java

interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}

Output:

hello nested interface

Example of nested interface which is declared within the class

Let's see how we can define an interface inside the class and how we can access it.

TestNestedInterface2.java

7. class A
8. {
9. interface Message
4. {
9. void msg();
10. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
16. public void msg(){System.out.println("Hello nested interface");
17. }
18. public static void main(String args[])
13. {
22. A.Message message=new TestNestedInterface2();//upcasting here
23. message.msg();
24. }
25. }

Output:

hello nested interface

Variables in Java Interfaces

In java, an interface is a completely abstract class. An interface is a container of abstract


methods and static final variables. The interface contains the static final variables. The
variables defined in an interface cannot be modified by the class that implements the
interface, but it may use as it defined in the interface.

🔔 The variable in an interface is public, static, and final by default.


🔔 If any variable in an interface is defined without public, static, and final keywords, then
the compiler automatically adds the same.
🔔 No access modifier is allowed except the public for interface variables.
🔔 Every variable of an interface must be initialized in the interface itself.
🔔 The class that implements an interface cannot modify the interface variable, but it may be
used as it is defined in the interface.

interface SampleInterface{

int UPPER_LIMIT = 100;


}
public class InterfaceVariablesExample implements SampleInterface{

public static void main(String[] args)


{
UPPER_LIMIT = 150; // Can not be modified

System.out.println("UPPER LIMIT = " + UPPER_LIMIT);


}

}
Extending an Interface in java

In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.

A class implements an interface, but one interface extends another interface.

19. interface Printable{


20. void print();
21. }
22. interface Showable extends Printable{
23. void show();
24. }
25. class TestInterface4 implements Showable{
26. public void print(){System.out.println("Hello");}
27. public void show(){System.out.println("Welcome");}
10.
23. public static void main(String args[]){
24. TestInterface4 obj = new TestInterface4();
25. obj.print();
26. obj.show();
27. }
28. }

Output:

You might also like