Professional Documents
Culture Documents
Packages and Interfaces: CS F213: Object Oriented Programming
Packages and Interfaces: CS F213: Object Oriented Programming
Dipanjan Chakraborty
Department of CS&IS
1
Packages
●
Java’s mechanism to partition the class namespace
– Can I have two Rectangle classes? What if another
programmer needs to write a different Rectangle
class?
●
Also a visibility mechanism
– Can write code, or class members only visible to
other classes of the same package
2
Packages
●
Syntax: use the package command as the first statment in a Java source file
●
If no package statement is included in a source file, then it belongs to the default package which
has no name
– E.g.: package mypackage;
●
The convention is to follow the same hierarchy in package and directories
– Package com.java.source would corresspond to directory hierarchy com/java/source
– Some IDEs like Eclipse forces packages to match the directory hierarchy
● Using javac -d . Sample.java would create the directory hierarchy to map the package
name
●
Java searches for classes in the current directory or in the path(s) specified in the environment
variable CLASSPATH or the classpath(s) passed to the -classpath option in java or javac
●
Let’s take an example
3
Packages: accessing members
●
Recall: no access modifier (default access
modifier)
– Members can be accessed by other classes
in the same package
4
Importing packages
●
Import statment(s) follow right after the
package statement, and before any class
definitions
– Alternately, the dot separated package
names can be used to refer to classes
●
Package java.lang.* is implicitly imported in all
Java programs
5
Interfaces
●
Fully abstracts a class’ interface from its implementation
●
One can specify what a class must do, but not how it does it
●
Interfaces lack instance variables
●
Generally methods do not have any body
●
Any number of classes can implement an interface, and one
class can implement multiple interfaces
– Contrast with abstract classes
●
A Class must provide the complete set of methods required by the
interface
6
Advantages of Interfaces
●
Supports dynamic method resolution at run
time
●
Disconnect the definition of a method from the
inheritance hierarchy
7
General format of Interfaces
1 access interface name {
2 return-type method-name1(parameter-list);
3 return-type method-name2(parameter-list);
4
5 type final-varname1 = value;
6 type final-varname2 = value;
7 //...
8 return-type method-nameN(parameter-list);
9 type final-varnameN = value;
10 }
●
Access modifiers follow the same rules as classes, must
be public or default
●
Any class implementing the interface must define all of
the methods 8
General format of Interfaces
●
All variables are implicitly static and final
●
All methods and variables are implicitly public
●
Prior to JDK 8 no method implementation was supported
in interfaces
– Starting with JDK 8 interfaces may include a default
implementation
●
Starting with JDK 8 interfaces can have static methods
●
Starting with JDK 9 interfaces can have private methods
9
Implementing Interfaces
1 class classname [extends superclass] [implements interface [,interface...]] {
2 // class-body
3 }
●
Methods that implement an interface must be declared public.
●
The type signature of the implementing method must match
exactly the type signature specified in the interface definition
●
A class implementing an interface may define additional
methods as well
●
If a class includes an interface but does not fully implement the
methods required by that interface, then that class must be
declared as abstract 10
Interface References
●
Variables can be declared as interface reference
types
Shape sh = new Square2(5);
●
The method to be executed is resolved dynamically
at run time
●
An interface reference variable has knowledge only
of the methods declared by its interface declaration
11
Nested Interfaces
●
An interface can be declared as a member of a
class or of another interface
●
Can be public, private or protected
●
Outside of the class or interface in which a
nested interface is declared, its name must be
fully qualified
12
Nested Interfaces
1 class A { 1 class B implements A.NestedIF {
2 public interface NestedIF { 2 public boolean isNotNegative(int x) {
3 boolean isNotNegative(int x); 3 return x < 0 ? false: true;
4 } 4 }
5 } 5 }
1 class NestedIFDemo {
2 public static void main(String[] args) {
3 A.NestedIF nif = new B();
4 if(nif.isNotNegative(10))
5 System.out.println("10 is not negative");
6 if(nif.isNotNegative(-12))
7 System.out.println("this won't be displayed");
8 }
9 }
13
Variables in Interfaces
●
An interface can define variables. When no
other methods are declared, the interface can
be used to share constants across different
classes
1 public interface Constants {
2 double pi = 3.14;
3 double g = 9.8;
4 double avogadros = 6.023*10e23;
5 }
14
Inheriting Interfaces
●
An interface can inherit another interface
using the extends keyword
●
Any class that implements an interface must
implement all methods required by that
interface, including any that are inherited from
other interfaces.
15
Default Methods in Interfaces
●
JDK 8 onwards, Java allows interfaces to have a default
method implementation
●
Prevents older codes implementing an interface from
breaking when a new method is introduced in the interface
●
Allows classes to implement interfaces without bothering
about methods not relevant to it
● Default method in interfaces is preceded by the default
keyword
16
Default Methods in Interfaces
●
If a class overrides a default method in an interface, the class’ method
takes precedence
●
If a class implements two interfaces both of which have default methods of
the same name:
– If class overrides the default method, the class’ method takes
precedence
– If class does not override the default method, then there would be an
error
– Or, name conflicts can be resolved using:
InterfaceName.super.methodName( )
●
In case of inheritance, the subclass’ default method takes precedence
17
Static Interface Methods
●
Interfaces can have static methods since JDK 8
●
These can be accessed without any instances,
using the interface name and a period (.)
separator
– InterfaceName.method()
●
Static methods are not inherited by either
implementing classes or extending interfaces
18
Private Interface Methods
●
Interfaces can have private methods since JDK
9
●
Can be called only by default methods or other
private methods in the same interface
– Enables default methods to reuse code
without code duplication
19
Differences between Interaces and Abstract Classes
●
An abstract class can have 0 or more abstract
methods in addition to concrete methods
●
An interface can not have any public concrete
methods
●
A class can extend only one abstract class, but
can implement multiple interfaces
20