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

Lecture 14 Interfaces and Packages

Uploaded by

Abcd Efgh
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)
3 views

Lecture 14 Interfaces and Packages

Uploaded by

Abcd Efgh
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/ 27

Maharaja Agrasen Institute of

Technology
CIC-212 Java Programming

Interfaces, Packages
Interfaces
Concept
An interface is a way to describe what classes should
do, without specifying how they should do it. It’s not a class
but a set of requirements for classes that want to conform
to the interface
E.g. public interface Comparable
{
int compareTo(Object
otherObject);
}

It requires that any class implementing the Comparable interface contains a


compareTo method, and this method must take an Object parameter and
return an integer
Interface declarations

 The declaration consists of a keyword interface,


its name, and the members
 Similar to classes, interfaces can have three types
of members
 constants (fields)
 methods
 nested classes and interfaces
Interface member – constants
 An interface can define named constants, which are public,
static and final (these modifiers are omitted by
convention) automatically. Interfaces never contain instant
fields.
 All the named constants MUST be initialized
An example interface
Interface Verbose {
int SILENT = 0;
int TERSE = 1;
int NORMAL = 2;
int VERBOSE = 3;
void setVerbosity (int level);
int getVerbosity();
}
Interface member – methods
 They are implicitly abstract (omitted by
convention). So every method declaration consists
of the method header and a semicolon.
 They are implicitly public (omitted by
convention). No other types of access modifiers are
allowed.
 They can’t be final, nor static
 An interface can have different modifiers as follows
– public/package(default)
– abstract
• all interfaces are implicitly abstract
• omitted by convention
How to implement interfaces in a class
Two steps to make a class implement an interface
1. declare that the class intends to implement the given
interface by using the implements keyword

class Employee implements Comparable { . . . }

2. supply definitions for all methods in the interface


public int compareTo(Object otherObject) {
Employee other = (Employee) otherObject;
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0; }

note: in the Comparable interface declaration, the method


compareTo() is public implicitly but this modifier is omitted. But in
the Employee class design, you cannot omit the public modifier,
otherwise, it will be assumed to have package accessibility
How to implement interfaces in a class
 If a class leaves any method of the interface
undefined, the class becomes abstract class
and must be declared abstract.
 A single class can implement multiple interfaces.
Just separate the interface names by comma

class Employee implements Comparable, Cloneable {.


. .}
Instantiation properties of interfaces
 Interfaces are not classes. You can never use the
new operator to instantiate an interface.
public interface Comparable {
. . . }
Comparable x = new Comparable( );
 You can still declare interface variables
Comparable x;

but they must refer to an object of a class that


implements the interface
class Employee implements Comparable {
. . .
}
x = new Employee( );
Extending interfaces
 Interfaces support multiple inheritance – an interface can
extend more than one interface, Superinterfaces and
subinterfaces.
Example
public interface SerializableRunnable extends
java.io.Serializable, Runnable {
. . . }
 An extended interface inherits all the constants from its
superinterfaces.
 Take care when the subinterface inherits more than one
constants with the same name, or the subinterface and
superinterface contain constants with the same name —
always use sufficient enough information to refer to the target
constants.
Tedious Details
 When an interface inherits two or more constants
with the same name
– In the subinterface, explicitly use the superinterface name
to refer to the constant of that superinterface

interface A {
int val = 1;
}
interface B {
int val = 2;
}
interface C extends A, B {
System.out.println(“A.val = “+
A.val); System.out.println(“B.val = “+
B.val);
}
Tedious Details
 If a superinterface and a subinterface contain two constants
with the same name, then the one belonging to the
superinterface is hidden.
1. in the subinterface
– access the subinterface-version constants by directly using its
name
– access the superinterface-version constants by using the
superinterface name followed by a dot and then the constant
name
interface X { Y‘s val
int val = 1; }
X’s val
interface Y extends X{
int val = 2;
int sum = val + X.val; }
2. outside the subinterface and the superinterface
– you can access both of the constants by explicitly giving the
interface name.
In previous example, use Y.val and Y.sum to access constants
val and sum of interface Y, and use X.val to access constant
val of interface X.
Tedious Details
 When a superinterface and a subinterface contain two constants
with the same name, and a class implements the subinterface
– the class inherits the subinterface-version constants as its static
fields. Their access follow the rule of class’s static fields access.
class Z implements Y { }
//inside the class
System.out.println(“Z.val:“+val); //Z.val = 2
//outside the class
System.out.println(“Z.val:“+Z.val); //Z.val = 2
— object reference can be used to access the constants
 subinterface-version constants are accessed by using the object
reference followed by a dot followed by the constant name
 superinterface-version constants are accessed by explicit casting
Z v = new Z( );
System.out.print( “v.val = “ + v.val + “, ((Y)v).val = “ + ((Y)v).val
+“, ((X)v).val = “ + ((X)v).val );

output: v.val = 2, ((Y)v).val = 2, ((X)v).val = 1


Extending interfaces – about methods
 If a declared method in a subinterface has the same signature as an
inherited method and the same return type, then the new declaration
overrides the inherited method in its superinterface. If the only
difference is in the return type, then there will be a compile-time error
 An interface can inherit more than one methods with the same
signature and return type. A class can implement different
interfaces containing methods with the same signature and return
type.
 Overriding in interfaces has NO question of ambiguity. The real
behavior is ultimately decided by the implementation in the class
implementing them. The real issue is whether a single
implementation can honor all the contracts implied by that method in
different interfaces
 Methods with same name but different parameter lists are
overloaded
Interface vs. abstract class

Interface Abstract class


Fields Only constants Constants and variable
data

Methods No implementation Abstract or concrete


allowed (no
abstract modifier
necessary)
Interface vs. abstract class
Interface Abstract class
Inheritance A subclass can A subclass can inherit
implement many only one class
interfaces

Can extend Can implement


numerous numerous interfaces
interfaces
Cannot extend a Extends one class
class
Interface vs. abstract class
Interface Abstract class
Root none Object (of all
classes)
names Adjective or Nouns Nouns
Packages
Concept
Packages are nothing more than the way we organize files
into different directories according to their functionality,
usability as well as category they should belong to .
A Java package is a Java programming language
mechanism for organizing classes into namespaces.
Java source files belonging to the same category or
providing similar functionality can include a package
statement at the top of the file to designate the package for
the classes the source file defines.

Java packages can be stored in compressed files called


JAR files.
Concept
Packaging also help us to avoid class name collision when
we use the same class name as that of others.

For example, if we have a class name called "Vector", its


name would crash with the Vector class from JDK.
However, this never happens because JDK uses java.util
as a package name for the Vector class (java.util.Vector ).

Understanding the concept of a package will also help us


manage and use files stored in jar files in more efficient
ways.
Concept

JAVA PACKAGE

JAVA API USER DEFINED


PACKAGE PACKAGE

lang util io awt applet


Using Packages
• To use a package inside a Java source file, it is
convenient to import the classes from the
package with an import statement.

import java.awt.*;
import java.awt.Colour;

• The above first statement imports all classes


from the java.awt package and second will
import only Colour class.
Creation Of Jar Files
• In Java source files the package the file
belongs to is specified with the package
keyword .
package java.awt.event;

• JAR Files are created with the jar command-line


utility.

• The command “jar cf myPackage.jar


*.class” compresses all *.class files into the
JAR file myPackage.jar.
Package Naming
Conventions
 Packages are usually defined using a hierarchical
naming pattern, with levels in the hierarchy separated
by periods (.) .
 Although packages lower in the naming
hierarchy are often referred to a "subpackages"
of the corresponding packages higher in the
hierarchy, there is no semantic relationship
between packages.
 Package names should be all lowercase characters
whenever possible.
 Frequently a package name begins with the top
level domain name of the organization and then
the organization's domain and then any
subdomains listed in reverse order.
 The organization can then choose a specific name for
their package.
Creating a Package
Package firstpack; 1. Declare the pkg. at the beginning of the file.
2. Save the file with the same name as the class
name.
Public class Firstclass 3. Save it in the directory having same name as
{ the package name and that directory should
be the subdirectory of the directory where all
the source files are saved.
} 4. Compile the file. It will create .class file in the
class Secondclass subdirectory.
{
Java also support the concept of pkg. hierarchy.
} Package p1.p2.p3;
It should be saved in directory p1/p2/p3.
class Thirdclass
{

} Java pkg. file have more than one class definition.


But only one class will be declared as public.
Compilation will leads to independent .class files
corresponding to each class.
Using user defined
Package
package p1; import p1.ClassA;

public class ClassA class Pkgtest


{ {

public void displayA() public static void main(String[]


{ args)
System.out.println(“Class {
A”); ClassA ob = new ClassA();
} ob.displayA();
}
}
}

You might also like