CSEg2202
Object Oriented Programming
CHAPTER - 4
Package and Interface
2023
Content of the lecture
Introduction to interface
Syntax and properties
Implementing interface with example
Multiple inheritance in java
Inner class
Package
Interface
Interface is a collection of public static final variables (constants) and abstract
methods.
Interface is a collection of universal common reusable data members and
methods .
A programmer uses an abstract class when there are some common features
shared by all the objects.
A programmer writes an interface when all the features have different
implementations for different objects.
Interface
Interfaces are written when the programmer wants to leave the implementation to
third party vendors.
An interface is a specification of method prototypes.
An interface contains zero or more abstract methods and all the methods of
interface are public, abstract by default.
Defining Interface
Access can be public or default
The methods which are declared have
no bodies(abstract).
They end with a semicolon after the
parameter list.
Each class that includes an interface
must implement all of the methods.
Rules of Interface
It does not implement any methods
A class that implement an interface must implement all its methods
Interface can not be instantiated
Any method defined under the interface become public & abstract
Any variable also become final and static even if you are not explicitly mention.
Properties of Interface
Interface is a keyword used for developing universal user defined data type
Each method in an interface is also implicitly abstract, so the abstract
keyword is not needed.
Methods in an interface are implicitly public.
All the data members of interface are implicitly public static final.
It does not contain any constructors.
All methods in an interface are abstract.
Interface - Example
Interface - Example
Interface - Example
Interface – Example (2)
Interface – Example (2)
Multiple Inheritance in Java
Java does not support multiple inheritance. But it can be achieved by using
interfaces.
Interface Vs Abstract Classes
Interface Abstract Class
Methods can be declared Methods can be declared
No method bodies Method bodies can be defined
Constants can be declared Can have constructors
Has no constructors All types of variables can be declared
Multiple inheritance possible Multiple inheritance not possible
Multiple “parent” interfaces Always inherits from Object
Only one “parent” class
Inner/Nested Class
In Java, it is also possible to nest classes (a class within a class).
The purpose of nested classes is to group classes that belong together,
which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and then create an object of
the inner class
Inner/Nested Class - Example
Inner/Nested Class – Example (2)
Package
Package is a collection of related classes, interface and sub package .
If any class or interface is common for many number of java programmers, we place them in
the packages
or in other words all the classes and interface of a package are common for all the
java programmers.
A package represents a directory that contains related group of classes and interfaces.
In java packages are classified into 3 types
1) pre defined packages
2) user/programmer defined packages
3) Third party packages
Package
Pre defined packages
Pre defined packages are those developed by sun micro system and supplied as a part of jdk to deal with
common requirements .
[Link].*;
[Link].*;
[Link].*;
User defined packages.
Are those which are developed by java programmers and are supplied as a part of their project to deal with
common requirements
Third party packages.
are those developed by third party software vendors and released to real industry as part of products.
Example [Link].
Package
Syntax for creating a package:
package package_name;
Example: package pack;
The package creation statement must be the first statement in the program while creating a
package.
And, declare all the members and the class itself as public
then only the public members are available outside the package to other programs.
When the package statement is omitted, class names are put into the default package which
has no name.
Creating a Package
Package - Guidelines
For placing classes and interface in the packages, sun micro system developers has
prescribed the following guide lines .
choose an appropriate packages name for placing class and interface for common
classes and interface.
choose an appropriate class name and interface name and ensure whose modifier
must be public.
the modifier of the method of the class/interface which present in the packages must
be public.
the modifier of the constructor of the class which present in the packages must be
public ( this rule is not applicable in the case of interface).
Package - Guidelines
Since classes within packages must be fully-qualified with their package
names, it would be tedious to always type long dot-separated names.
The import statement allows to use classes or whole packages directly.
Importing of a concrete class:
Importing of all classes within a package:
The import statement occurs immediately after the package statement and
before the class statement.
Package - Example
Package - Example
Package - Example
Package - Fully qualified name approach
It is one of the alternative approach for referring the classes and interface of the
packages instead of import statement (keyword).
This approach can refer either a class or interface but not possible to refer all the
classes and interface of the packages.
Syntax
pack1.pack2.class_name or
pack1.pack2.interface_name
Example
class test extends [Link]
{
}
Access Protection
Specifies the scope of the data members, class and methods.
private members of the class are available with in the class only. The scope of private
members of the class is “CLASS SCOPE”.
public members of the class are available anywhere . The scope of public members of
the class is "GLOBAL SCOPE".
default members of the class are available with in the class, outside the class and in its
sub class of same package. It is not available outside the package.
So the scope of default members of the class is "PACKAGE SCOPE".
protected members of the class are available with in the class, outside the class and in
its sub class of same package and also available to subclasses in different package also.
protected access modifier
The protected access modifier is accessible within package and outside
the package but through inheritance only.
The protected access modifier can be applied on the data member,
method and constructor. It can't be applied on the class
protected access modifier - Example
Access Modifier – Example (2)
Access Modifier – Example (2)
Adama Science and Technology University
School of Electrical Engineering And Computing 2023 G.C