Unit-III, P2
Unit-III, P2
UNIT-III, PART-II
Basics of Interface:
The interface in Java is a mechanism to achieve abstraction. It is used to achieve abstraction and
multiple inheritances in Java. An interface in Java is a blueprint of a behavior. It is used to specify
the behavior of a class.
In Java, an interface is a reference type, similar to a class, that can contain abstract methods
(methods without a body) and constants (static final variables).
1. Abstract Methods: An interface can declare methods, that methods don't have bodies.
These methods are implicitly public and abstract.
2. Constants: Variables declared in an interface are implicitly public, static, and
final.
3. Multiple Inheritance: Java does not support multiple inheritance through classes, but a
class can implement multiple interfaces, allowing multiple inheritance of type.
4. Implements Keyword: A class uses the implements keyword to implement an
interface.
Syntax of an Interface:
interface MyInterface {
// Constant variable
int MY_CONSTANT = 10; // implicitly public, static, final
// Abstract method
void myMethod(); // implicitly public and abstract
Example of an Interface:
// Define an interface
interface Animal {
// Abstract method
void sound(); // Every animal should implement this method
// Abstract method
void eat();
}
Abstract Methods: The Animal interface declares abstract methods sound() and eat().
Classes Dog and Cat implement the interface and provide concrete implementations of these
methods.
Implements Keyword: The Dog and Cat classes use the implements keyword to indicate
they are implementing the Animal interface.
Multiple Interfaces:
In Java, a class can implement multiple interfaces, which allows it to inherit behavior from multiple
interfaces. This is a form of multiple inheritance and is one of the key advantages of interfaces
in Java.
By using multiple interfaces, a class can implement methods from different interfaces. A class can
implement more than one interface by listing them after the implements keyword, separated by
commas.
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
@Override
public void swim() {
System.out.println("Duck is swimming");
}
}
Note:
The class Duck implements two interfaces: Flyable and Swimmable.
Example:
// Interface 1
interface SHAHID {
// Default method
default void show()
{
// Print statement
System.out.println("It is from Default method.");
}
}
Multilevel Interfaces:
Multi-level interfaces in Java refer to a situation where one interface extends another interface.
This allows one interface to inherit the methods of another interface, creating a chain of
inheritance. The interface that extends another interface can add new methods or override existing
default methods.
Syntax:
interface ParentInterface {
void method1(); // Abstract method
}
@Override
public void method2() {
System.out.println("Implementing method2");
} }
JAVA(UGCA-1932)
UNIT-III, PART-II
// Extended interface
interface Animal2 extends Animal {
void walk();
}
@Override
public void walk() {
System.out.println("Dog is walking");
}
@Override
public void bark() {
System.out.println("Dog is barking");
}
}
Explanation:
Animal Interface: The base interface that defines the eat() method.
Animal2 Interface: Extends the Animal interface and adds the walk() method.
Dog Interface: Extends the Animal2 interface and adds the bark() method.
JAVA(UGCA-1932)
UNIT-III, PART-II
Dod2 Class: Implements the Dog interface, so it must implement all the methods from the
entire chain of interfaces (eat(), walk(), and bark()).
Conclusion:
Multi-level interfaces are a powerful design tool in Java that enables interface inheritance. They
provide a clean and modular way to design a flexible and extensible system where common
behaviors can be defined in base interfaces and specialized behaviors can be added in extended
interfaces.
This approach is useful when you want to define common functionality in base interfaces and then
extend it in more specialized interfaces.
Packages in Java:
A set of classes and interfaces grouped together are known as Packages in JAVA. The name itself
defines that pack (group) of related types such as classes and interfaces. Every class is a part of a
certain package. When you need to use an existing class, you need to add the package within the
Java program.
In Java, packages are used to group related classes and interfaces, providing a modular and
organized structure for managing large codebases. They improve code maintainability and control
access to class and interfaces.
Features of Packages:
Types of Packages:
1. Built-in Packages: These are provided by the Java API, such as java.util, java.io,
java.lang, etc.
java.util: Contains utility classes like ArrayList, HashMap, Date, and Collections.
java.io: Contains classes for input and output operations, such as File, BufferedReader,
PrintWriter, etc.
java.net: Provides classes for network operations like Socket, URL,
HttpURLConnection, etc.
java.sql: Provides classes for database connectivity, such as Connection, Statement,
and ResultSet.
Importing/accessing Packages:
To use classes or interfaces from other packages, you can use the import keyword. Java
provides two ways to import packages:
2. User-defined Packages: These are created by developers to organize their own classes
and interfaces. To create a user-defined package:
package file.java.shapes;
import file.java.shapes.Circle;
Conclusion:
Packages are a fundamental part of Java's structure, allowing for organized and maintainable code.
They help manage access control and provide a convenient way to reuse code across projects.
Java's built-in packages, along with user-defined packages, offer a robust way to manage large
applications effectively.
1. Static Import
The static import feature (introduced in Java 5) allows the static members (fields and
methods) of a class to be imported directly, so they can be accessed without the class name
prefix. This can make the code cleaner and more concise, especially when you have to use a lot
of static members from a particular class.
import java.lang.Math;
public class Main {
JAVA(UGCA-1932)
UNIT-III, PART-II
In this example, the sqrt() method from the Math class can be accessed directly without the
Math prefix because of the static import.
You can import all static members of a class using the * wildcard.
Here, both sqrt() and pow() methods are used without the Math class prefix, because of static
import.
JAVA(UGCA-1932)
UNIT-III, PART-II
2. Package Class
A package class refers to any class that belongs to a package. In Java, classes are grouped into
packages to organize and modularize the code. These package classes can be either built-in classes
from the Java API or user-defined classes that developers create within their own packages.