You are on page 1of 6

Advanced Programming – 1

_____________________________________________________________________________________

INTERFACES

Multiple inheritance:
Deriving new class from more than one super class is known as multiple inheritance.

INTERFACE:
An interface is a group of method declarations. An interface contains abstract methods and final variables. We can
define an interface by using the keyword interface as shown below.
Syn: interface interface – name
{
final variables;
abstract methods;
}
Interfaces have the following characteristics. They are
 An interface should be declared by using the key word interface.
 The main advantage of interface is that we can achieve multiple inheritance.
 An interface is a group of method declarations. That means, only method names are written in the interface
without method body.
 An interface will have 0 or more abstract methods.
 The methods of interface are public and abstract by default.
 The variables of interface are public, static and final by default.
 We cannot create objects to the interface. But we can create a reference of interface.
 All methods of interfaces should be implemented in its implementation classes.
 We can also extend interfaces. That means we can create new interface from old interface.
 An interface cannot implement another interface.
 A class can implement multiple interfaces.

Implementing interfaces:
It is not possible to create objects to the interfaces. So we should implement all the methods of the interfaces by
using a class. This class is called as implementation class.
It is necessary to create implementation classes to provide body for the abstract methods of interfaces. We can create
implementation classes to the interfaces by using the keyword implements as shown below.
Syn: class class – name implements interface1, interface2, interface3…{
Body for interface methods;
}
When a class implements more than one interface, then they are separated by a comma’s.
EX: interface Area {
double PI=3.14;
void calculate(double x);
}
class Circle implements Area {
public void calculate(double x) {
double area=PI*x*x;
System.out.println("Area of the Circle = "+area);
}
}
class Square implements Area {
public void calculate(double x) {
double area=x*x;

Prepared by Krishna Kumari Ganga Page 1


Advanced Programming – 1
_____________________________________________________________________________________

System.out.println("Area of the Square = "+area);


}
}
class InterfaceEx {
public static void main(String[] args) {
Circle c=new Circle();
Square s=new Square();
c.calculate(5.6);
s.calculate(7.8);
}
}

Extending interfaces:
Like classes we can also create a new interface from an old interface. The new interface is called as sub interface
(or) child interface (or) derived interface. The old interface is called as super interface (or) parent interface (or) base
interface.
We can extend an interface by using the keyword extends as shown below.
Syn: interface interface – name extends interface1, interface2, interface3…{
Sub interface final variables;
Sub interface abstract methods;
}
Here, extends keyword specifying that all super interfaces members are inherited in to sub interface. So, the sub
interface contains its own members and also super interface members.
We can also create a class as a sub class to another class and as an implementation class to the one or more
interfaces as shown below.
SYN: interface class - name extends class – name implements interface1, interface2, interface3…{
Data;
Methods;
}
In this case, we must use extends keyword first and then secondly use the keyword implements.
Ex: interface A {
void meth1();
void meth2();
}
interface B extends A {
void meth3();
}
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

Prepared by Krishna Kumari Ganga Page 2


Advanced Programming – 1
_____________________________________________________________________________________

Q. What are the differences between abstract class and an interface?


Abstract class Interface
1. An abstract class is declared by using the keyword 1. An interface is declared by using a keyword interface.
abstract. .
2. All the abstract methods of abstract class should be 2. All the abstract methods of an interface should be
implemented in its sub class. implemented in its implementation class.
3. An abstract class contains instance variables and final 3. An interface cannot contain instance variables. It
variables. contains only final variables.
4. An abstract class contains some abstract methods and 4. An interface cannot contain concrete methods. It
some concrete methods. contains only abstract methods.
5. If an abstract class is created, then it is the duty of the 5. An interface is created when the programmer wants to
programmer to provide sub class to it. leave the implementation to the third party vendors.

Prepared by Krishna Kumari Ganga Page 3


Advanced Programming – 1
_____________________________________________________________________________________

Packages

Definition:
A package represents a directory (folder) that contains a group of related classes and interfaces.

Benefits:
 The main advantage of packages is code reusability.
 Packages provide a way for separating Design and Coding.
 Packages provide a way for hiding the classes and interfaces for preventing other program or packages
from accessing.
 Two classes in two different packages can have the same name.

TYPES OF PACKAGES:
There are two types of packages available in java language. They are
1. Pre – defined (or) built – in packages
2. User – defined packages

1. Pre – defined (or) built – in packages:


The packages which are defined by the java developers are called as pre – defined or built – in packages. There are
several built – in packages are available in java. Some of the commonly used packages are listed below.

1. java.lang: This package provides a collection of classes and interfaces to implement a basic java program.
2. java.io: This package provides a collection of classes and interfaces for Input/Output manipulations.
3. java.util: This package provides a collection of classes and interfaces for utility functions such as date and time
functions.
4. java.applet: This package provides a collection of classes and interfaces to create java applets
5. java.awt: awt stands for Abstract Window tool Kit. This package provides a collection of classes and interfaces
to implement system – independent graphical user interface.
6. java.net: This package provides a collection of classes and interfaces to communicate with other computers via
Internet.
7. java.sql: sql stands for structured query language. This package provides a collection of classes and interfaces to
communicate the java program with the data base.

2. User – defined packages:


The packages which are defined by the user are called as user – defined packages. We can create any number of our
own packages in java language.

Creating a package:
It is very easy to create a package in java language. We have to follow the following steps to create a package.

Prepared by Krishna Kumari Ganga Page 4


Advanced Programming – 1
_____________________________________________________________________________________

Step – 1: Specify the package name by using the keyword package at the beginning of the program as shown below.
package mypack;
Step – 2: Define a class which one we want to store in the package and declare it as public as shown below.

package mypack;
public class Addition
{
public void add(int a,int b)
{
int res = a+b;
System.out.println(“Addition of a,b=”+res);
}
}
Step – 3: Save the program with class – name and with .java extension (Addition.java).

Step – 4: Compile the java program by using java compiler as shown below.
javac –d . Addition.java
 -d represents make a directory
 . represents the present location
Conclusion:
Java Compiler creates a directory with the specified package name and places the .class file under it.

Adding a class to the package:


It is also very easy to add a class in to the existing package. We have to follow the following steps to add a class in
to the package.
Step – 1: Specify the package name by using the keyword package at the beginning of the program as shown below.
package mypack;
Step – 2: Define a class which one we want to add in to the package and declare it as public as shown below.
package mypack;
public class Subtraction
{
public void sub(int a,int b)
{
int res = a-b;
System.out.println(“Subtraction of a,b=”+res);
}
}
Step – 3: Save the program with class – name and with .java extension (Subtraction.java).

Prepared by Krishna Kumari Ganga Page 5


Advanced Programming – 1
_____________________________________________________________________________________

Step – 4: Compile the java program by using java compiler as shown below.
javac –d . Subtraction.java
 -d represents make a directory
 . represents the present location

Conclusion:
Java Compiler creates .class file and places it in the specified package(mypack).

ACCESSING PACKAGES:
We can access the packages by using the keyword import. There are two ways to access the classes and interfaces of
a package in to the program. They are
 By specifying the fully qualified class – name in the package as shown below.
import package – name.class – name;
Ex: import mypack.Addition;
Import mypack.Subtraction;
 By specifying * symbal instead of the class – name in the package as shown below.
import package – name.*;
Ex: import mypack.*;
Here, * indicates all classes and interfaces in the specified package.
Note: In this case, both .java and .class files must be placed with in the package.

HIDING THE CLASSES:


If we want to hide the classes from accessing from outside of the package, then we should declare the classes as non
– public. These non – public classes are accessible only with in the package.
package mypack;
public class Multiplication {
public void mul(int a,int b) {
int res = a*b;
System.out.println(“Multiplication of a,b=”+res);
}
}

Prepared by Krishna Kumari Ganga Page 6

You might also like