Probability and Statistics
Probability and Statistics
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.
8
Defining and Implementing an Interface
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.
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.
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;
21
Creating and Accessing a Package
22
Using the Package in Another Program
25
📌 Importing Packages
import mystudents.Student;
import mystudents.*;
26