0% found this document useful (0 votes)
2 views

Probability and Statistics

This document covers Java Interfaces and Packages, detailing the differences between interfaces and abstract classes, how to define and implement interfaces, and the concept of packages in Java. It explains the syntax for creating interfaces, implementing multiple interfaces, and the process of defining and using packages, including built-in and user-defined packages. Additionally, it discusses the CLASSPATH environment variable and methods for importing packages in Java programs.

Uploaded by

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

Probability and Statistics

This document covers Java Interfaces and Packages, detailing the differences between interfaces and abstract classes, how to define and implement interfaces, and the concept of packages in Java. It explains the syntax for creating interfaces, implementing multiple interfaces, and the process of defining and using packages, including built-in and user-defined packages. Additionally, it discusses the CLASSPATH environment variable and methods for importing packages in Java programs.

Uploaded by

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

Java

Unit-3
Interfaces and Packages

Dr S
Jayanthi
UNIT-III

Interface:
o Interfaces VS Abstract classes,
o defining an interface,
o implement interfaces,
o accessing implementations through interface references,
o extending interface
Packages:
o Defining, creating and accessing a package,
understanding CLASSPATH,
o importing packages

2
Interfaces vs. Abstract Classes
Feature Interface Abstract Class
A class that can have both
A blueprint that defines
abstract (without
Definition methods but does not
implementation) and concrete
provide implementation.
methods.
Method
Only method signatures, Can have both abstract and
Implement
no implementation. concrete methods.
ation
Only public, static, and Can have instance variables
Fields
final (constants). (non-static, non-final).
A class can implement
A class can inherit only one
multiple interfaces
Inheritance abstract class (single
(supports multiple
inheritance).
inheritance).
Methods can have any access
Access Methods are always 3
Interfaces vs. Abstract Classes

When to Use?
o Use Interfaces for defining behavior that multiple classes
should implement.
o Use Abstract Classes when you need to share some
common behavior among related classes.

4
INTERFACE IN JAVA
o An interface looks similar to a class but is not a class. It
acts as a blueprint for other classes.
o An interface can have methods and constant fields,
just like a class.
o However, all methods in an interface are abstract by
default (only method signatures, no implementation).
o The fields declared in an interface are always public,
static, and final by default.
o The methods are always public and abstract by default
(until Java 8, which introduced default methods).
o Interfaces provide total abstraction, meaning they do
not contain any method implementations.
o A class implements an interface by providing
implementations for all its methods. 5
Syntax: Interface

interface InterfaceName
{
// constant variable declarations
// abstract method declarations
}
The interface mechanism is used to achieve abstraction and multiple
inheritance.

6
Defining and Implementing an Interface

Defining an Interface
An interface defines a contract that a class must
follow.
interface Student {
void study(); // Abstract method (implicitly public
and abstract)
}

7
Defining and Implementing an Interface
Implementing an Interface
A class that implements the interface must provide
implementations for all abstract methods.

class CollegeStudent implements Student {


public void study() {
System.out.println("College student is
studying...");
}
}

8
Defining and Implementing an Interface

Accessing Implementation Through Interface


References
Using an interface reference to access the
public class StuInterface
implementation class.{
public static void main(String[] args) {
Student student = new CollegeStudent(); // Using
interface reference
student.study(); // Calls CollegeStudent's implementation
of study()
}
}

9
Defining and Implementing an Interface
interface Student {
void study(); // Abstract method (implicitly public and abstract)
}
class CollegeStudent implements Student {
public void study() {
System.out.println("College student is studying...");
}
}
public class StuInterface {
public static void main(String[] args) {
Student s = new CollegeStudent(); // Using interface reference
s.study(); // Calls CollegeStudent's implementation of study()
}
}

10
Implementing Multiple Interface
 A class can implement more than one interface. For this, you write all
interfaces that the class implements separated by commas following the
implements keyword. The class body must provide implementations for all
of the methods specified in the interfaces.

 The syntax for implementing multiple interfaces is


public class className implements interface1, interface2, interfaceN
{
// Provide implementation for methods of
// all included interfaces
// ....................
}

11
interface Printable {
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();
}
}

12
interface Printable {
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[]) {
// Using class reference (allows both methods)
Process obj = new Process();
obj.print();
obj.show();
} } 13
Extending an Interface
 One interface can extend another but an interface can’t
implement any other interface; A class only can
implement interface.
As shown in the figure given below, a class extends another class, an
interface extends another interface, but a class implements an
interface.

14
Extending an
public class ExamMain {
Interface public static void main(String[] args) {
interface Exam { // Creating an object of
void takeExam(); StudentExam
} OnlineExam s = new
interface OnlineExam extends StudentExam();
Exam {
void submitOnline(); s.takeExam();
} s.submitOnline();
}
// Implementing the extended interface
}
class StudentExam implements OnlineExam {
public void takeExam() {
System.out.println("Student is taking the
exam...");
}
public void submitOnline() {
System.out.println("Student submitted the
exam online."); 15
Multiple Inheritance
interface Printable {
void print();
}
interface Showable { public static void main(String
void show(); } args[]) {
interface Display extends Showable, Process p = new
Printable { Process();
void disp(); p.print();
} p.show();
class Process implements Display { p.disp();
public void print() { }
System.out.println("Hello"); }
}
public void show() {
System.out.println("Welcome");
}
public void disp() {
System.out.println("Printing...");
16
Packages
 In java, a package is a container of classes, interfaces, and sub-
packages. Think of a package as a folder that contains Java
classes, just like how you organize files in different folders on
your computer.
 We use the packages to avoid naming conflicts and to organize
project-related classes, interfaces, and sub-packages into a
bundle.
 In java, the packages have divided into two types.
o Built-in Packages
o User-defined Packages

17
Built-in Packages
o The built-in packages are the packages from java API. The Java API is a
library of pre-defined classes, interfaces, and sub-packages. The built-in
packages were included in the JDK.
o There are many built-in packages in java, few of them are as java, lang, io,
util, awt, javax, swing, net, sql, etc.
o We need to import the built-in packages to use them in our program. To
import a package, we use the import statement.

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

19
Defining a Package in java
We use the package keyword to create or define a
package in java programming language.

Syntax package packageName;

20
Defining a Package
To define a package, use the package keyword at the very
beginning of the Java file.

// Define a package
package mystudents;

public class Student {


public void display() {
System.out.println("Hello,
Student!");
}
}

21
Creating and Accessing a Package

1. Save the file inside a folder named mystudents


2. Compile the Java file using:
javac -d . Student.java
o -d . tells the compiler to create the package in the current
directory.
o After compilation, a mystudents folder is created, containing
Student.class.

22
Using the Package in Another Program

To use the Student class from the mystudents package in


another Java program, we import it:
java // Importing the package
import mystudents.Student;

public class Main {


public static void main(String[] args)
{
Student s = new Student();
s.display();
}
}
23
Example
package myPackage;
public class DefiningPackage
{ public static void main(String[] args)
{
System.out.println("This class belongs to myPackage.");
}
}
Now, save the above code in a file DefiningPackage.java, and compile it
using the following command.
javac -d . DefiningPackage.java

The above command creates a directory with the package


name myPackage, and the DefiningPackage.class is saved into it.

Run the program use the following command.


java myPackage.DefiningPackage
24
Understanding CLASSPATH
What is CLASSPATH?
CLASSPATH is an environment variable that tells Java where to find user-
defined packages and external libraries.
Running a Java Program with a Package
After compiling, we need to run the Main class by specifying the
classpath:

java -cp . Main

🔹 -cp . tells Java to look in the current directory for packages.

25
📌 Importing Packages

1️Explicit Import (Specific Class)

import mystudents.Student;

This imports only the Student class from mystudents.

2️Wildcard Import (All Classes in a Package)

import mystudents.*;

This imports all classes inside the mystudents package.

26

You might also like