Chapter 4
Chapter 4
Chapter 4
CHAPTER - 4
Introduction to interface
Syntax and properties
Implementing interface with example
Multiple inheritance in java
Inner class
Package
Interface
Interfaces are written when the programmer wants to leave the implementation to
third party vendors.
An interface contains zero or more abstract methods and all the methods of
interface are public, abstract by default.
Defining 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
To access the inner class, create an object of the outer class, and then create an object of
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.
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:
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
}
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