Home 35s ago
Ask Kai Inter Medium
Java Unit-3: Interfaces and Packages
Interfaces and Packages in Java
Interfaces vs. Abstract Classes
Definition:
Interface: A blueprint that defines methods without providing
implementation.
Abstract Class: A class that can have both abstract methods (without
implementation) and concrete methods (with implementation).
Method Implementation:
Interface: Contains only method signatures.
Abstract Class: Can have both abstract and concrete methods.
Fields:
Interface: Only public, static, and final (constants).
Abstract Class: Can have instance variables (non-static, non-final).
Inheritance:
Interface: A class can implement multiple interfaces (supports
multiple inheritance).
Abstract Class: A class can inherit only one abstract class (single
inheritance).
Access Modifiers:
Interface: Methods are always public.
Abstract Class: Methods can have any access modifier (private,
protected, public).
When to Use:
Use Interfaces: To define behavior that multiple classes should
implement.
Use Abstract Classes: When needing to share some common behavior
among related classes.
Interface in Java
Characteristics:
An interface looks similar to a class but acts as a blueprint for other
classes.
It can have methods and constant fields like a class.
All methods are abstract by default until Java 8 introduced default
methods.
Fields are public, static, and final by default.
Syntax:
interface InterfaceName {
java
// constant variable declarations
// abstract method declarations
}
Defining and Implementing an Interface
Example:
// Defining an interface
java
interface Student {
void study(); // Abstract method
}
// Implementing the interface
class CollegeStudent implements Student {
public void study() {
System.out.println("College student is
studying...");
}
}
// Accessing implementation through interface
references
public class StuInterface {
public static void main(String[] args) {
Student student = new CollegeStudent();
student.study();
}
}
Implementing Multiple Interfaces
A class can implement more than one interface:
public class className implements interface1,
java
interface2, interfaceN {
// Provide implementation for methods of all
included interfaces
}
Example:
interface Printable {
java
void print();
}
interface Showable {
void show();
}
class Process implements Printable, Showable {
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("Welcome");
}
public static void main(String args[]) {
Printable p = new Process();
p.print();
Showable s = new Process();
s.show();
}
}
Extending an Interface
An interface can extend another interface:
interface Exam {
java
void takeExam();
}
interface OnlineExam extends Exam {
void submitOnline();
}
// Implementing the extended interface
class StudentExam implements OnlineExam {
public void takeExam() {
System.out.println("Student taking the
exam...");
}
public void submitOnline() {
System.out.println("Student submitted the
exam online.");
}
}
Packages in Java
Definition: A package is a container of classes, interfaces, and sub-
packages, similar to a folder on a computer.
Purpose:
To avoid naming conflicts.
To organize project-related classes, interfaces, and sub-packages into
a bundle.
Types of Packages
1. Built-in Packages:
Java API contains pre-defined classes, interfaces, and sub-packages
(e.g., java.lang, java.util).
Must import these packages using the import statement.
1. User-defined Packages:
Created by the user.
Defining a Package
Syntax:
package packageName;
java
Example:
package mystudents;
java
public class Student {
public void display() {
System.out.println("Hello, Student!");
}
}
Creating and Accessing a Package
Save the file inside a folder named mystudents
Compile using:
javac -d . Student.java
bash
To use the Student class from mystudents, import it:
import mystudents.Student;
java
Understanding CLASSPATH
What is CLASSPATH?: An environment variable that tells Java where
to find user-defined packages and external libraries.
Importing Packages