0% found this document useful (0 votes)
22 views32 pages

Interfaces

The document provides an overview of interfaces in programming, explaining their purpose, structure, and implementation in Java. It highlights the benefits of using interfaces, such as enabling multiple inheritance and reducing coupling between components. Additionally, it compares interfaces with abstract classes and discusses the concept of marker interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

Interfaces

The document provides an overview of interfaces in programming, explaining their purpose, structure, and implementation in Java. It highlights the benefits of using interfaces, such as enabling multiple inheritance and reducing coupling between components. Additionally, it compares interfaces with abstract classes and discusses the concept of marker interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Interfaces

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 1


Agenda

Introduction to interfaces

Applying Interfaces

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 2


Introduction to Interfaces

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 3


What is an Interface?
An interface is a named collection of method declarations (without implementations)

– An interface can also include constant declarations

– An interface is syntactically similar to an abstract class

– An interface is a collection of abstract methods and final variables

– A class implements an interface using the implements clause

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 4


What is an Interface? (Contd.).
• An interface defines a protocol of behavior

• An interface lays the specification of what a class is supposed to do

• How the behavior is implemented is the responsibility of each implementing class

• Any class that implements an interface adheres to the protocol defined by the interface, and
in the process, implements the specification laid down by the interface

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 5


Interface: Example
Calculate_salary
IN: emp_id String(10)
OUT: salary float (6,2)

Interface ‘lif_salary’

Write code to
Calculate salary
Write code to for India employees
Calculate salary
for US employees

class lcl_salary_US class lcl_salary_IN

Implements Implements
lif_salary lif_salary

Sheldon Rahul
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 6
Why interfaces are required ?
 Interfaces allow you to implement common behaviors in different classes that are not
related to each other

 Interfaces are used to describe behaviors that are not specific to any particular kind of
object, but common to several kind of objects

 Defining an interface has the advantage that an interface definition stands apart from any
class or class hierarchy

 This makes it possible for any number of independent classes to implement the interface

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 7


Why interfaces are required ? (Contd.).

 Thus, an interface is a means of specifying a consistent specification, the implementation of


which can be different across many independent and unrelated classes to suit the respective
needs of such classes

 Interfaces reduce coupling between components in your software

 Java does not support multiple inheritance

 This is a constraint in class design, as a class cannot achieve the functionality of two or
more classes at a time

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 8


Why interfaces are required ? (Contd.).
 Interfaces help us make up for this loss as a class can implement more than one interface at
a time

 Thus, interfaces enable you to create richer classes and at the same time the classes need
not be related

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 9


Interface members
 All the methods that are declared within an interface are always, by default, public and
abstract

 Any variable declared within. an interface is always, by default, public static and final

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 10


What will you choose..?

What is the behavior which is common among the entities depicted in the pictures above?

Yes..You are right. All of them can fly.

Requirement : You have to develop 3 classes, Bird, Superman and Aircraft with the condition that all these
classes must have a method called fly().

What is the mechanism, using which you can ensure that the method fly() is implemented in all these classes?
An Abstract class or An Interface?

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 11


Defining an Interface

 An interface is syntactically similar to a class

 It’s general form is:

public interface FirstInterface {


int addMethod(int x, int y);
float divMethod(int m, int n);
void display();
int VAR1 = 10;
float VAR2 = 20.65f;
}
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 12
Implementing Interfaces
• A class implements an interface
• A class can implement more than one interface by giving a comma- separated list of
interfaces
class MyClass implements FirstInterface{
public int addMethod(int a, int b){
return(a+b);
}
public float divMethod(int i, int j){
return(i/j);
}

public void display(){


System.out.println(“Variable 1 :” +VAR1);
System.out.println(“Variable 2 :” +VAR2);
}
}
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 13
Quiz
Will the following code compile successfully ?
interface I1 {
private int a=100;
protected void m1();
}

class A1 implements I1 {
public void m1() {
System.out.println(“In m1 method”);
}
}

It will throw compilation errors.. Why?

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 14


Quiz (Contd.).
Will the following code compile successfully ?
interface I1 {
static int a=100;
static void m1();
}

class A1 implements I1 {
public void m1() {
System.out.println(“In m1 method”);
}
}

It will throw compilation error.. Why?

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 15


Applying Interfaces

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 16


Applying Interfaces
 Software development is a process where constant changes are likely to happen

 There can be changes in requirement, changes in design, changes in implementation

 Interfaces support change

 Programming through interfaces helps create software solutions that are reusable, extensible,
and maintainable

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 17


Applying Interfaces (Contd.).
interface IntDemo{
void display();
}
class classOne implements IntDemo{
void add(int x, int y){
System.out.println("The sum is :" +(x+y));
}
public void display(){
System.out.println("Welcome to Interfaces");
}
}

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 18


Applying Interfaces (Contd.).
class classTwo implements IntDemo{
void multiply(int i,int j, int k) {
System.out.println("The result:" +(i*j*k) );
}
public void display(){
System.out.println("Welcome to Java ");
}
}
class DemoClass{
public static void main(String args[]) {
classOne c1= new classOne();
c1.add(10,20);
c1.display();
classTwo c2 = new classTwo();
c2.multiply(5,10,15);
c2.display();
}
} Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 19
Interface References
 When you create objects, you refer them through the class references. For example :
 ClassOne c1= new classOne(); /* Here, c1 refers to the object of the class
classOne. */
 You can also make the interface variable refer to the objects of the class that implements the
interface
 The exact method will be invoked at run time
 It helps us achieve run-time polymorphism

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 20


Interface References (Contd.).
interface IntDemo{
void display();
}
class classOne implements IntDemo{
void add(int x, int y){
System.out.println("The sum is :" +(x+y));
}
public void display(){
System.out.println("Class one display method ");
}
}

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 21


Interface References (Contd.).
class classTwo implements IntDemo {
void multiply(int i,int j, int k){
System.out.println("The result:" +(i*j*k) );
}
public void display(){
System.out.println("Class two display method" );
}
}
class DemoClass{
public static void main(String args[]){
IntDemo c1= new classOne();
c1.display();
c1 = new classTwo();
c1.display();
}
}
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 22
Extending Interfaces
 Just as classes can be inherited, interfaces can also be inherited

 One interface can extend one or more interfaces using the keyword extends

 When you implement an interface that extends another interface, you should provide
implementation for all the methods declared within the interface hierarchy

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 23


Marker Interface
 An Interface with no method declared in it, is known as Marker Interface

 Marker Interface is provided as a handle by java interpreter to mark a class, so that it can
provide special behavior to it at runtime

 Examples of Marker Interfaces :


 java.lang.Cloneable
 java.io.Serializable
 java.rmi.Remote

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 24


Quiz
Will the following code compile successfully ?
interface I1 {
int a=100;
void m1();
}

class A1 extends I1 {
public void m1() {
System.out.println(“In m1 method”);
}
}

It will throw compilation error.. Why?

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 25


Quiz (Contd.).
Will the following code compile successfully ?
interface I1 {
int a=100;
void m1();
}

interface A1 implements I1 {
public void m2();
}

It will throw compilation error.. Why?

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 26


Quiz (Contd.).
Will the following code compile successfully ?
interface I1 {
int a=100;
void m1();
}
interface A1 extends I1 {
public void m2();
}

class Aimp implements I1 {


public void m1() {
System.out.println(“In m1 method”);
}
}
This code will compile successfully..!
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 27
Abstract Classes v/s Interfaces
Abstract Classes Interfaces
Abstract classes can have non-final Variables declared within an
non-static variables. interface are always static and final.

Abstract Classes can have abstract Interfaces can have only method
methods as well as concrete declarations(abstract methods). You
methods. cannot define a concrete method.

You can declare any member of an Interface members are by default


abstract class as private, default, public. You cannot have private or
protected or public. Members can protected members. Interface
also be static. methods cannot be static.

Abstract class is extended by An interface is “implemented” by a


another class using “extends” java class using “implements”
keyword. keyword .
Contd..
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 28
Abstract Classes v/s Interfaces (Contd.).
Abstract Classes Interfaces
An abstract class can extend An interface can extend one or more
another class and it can implement interfaces but cannot extend a class.
one or more interfaces. It cannot implement an interface.

An abstract class can have You cannot define constructors


constructors defined within it. within an interface.

An abstract class cannot be An interface cannot be instantiated.


instantiated using “new” Keyword

You can execute(invoke) an You cannot execute an interface


abstract class, provided it has
public static void main(String[]
args) method declared within it.

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 29


Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 30
Summary

 Introduction to interfaces
 Creating interfaces
 Implementing interfaces
 Difference between interfaces and abstract classes

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 31


Thank You

Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 32

You might also like