Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Interface in Java

The document discusses interfaces in Java, including defining an interface, implementing an interface, and using interfaces to achieve multiple inheritance. It provides examples of defining a Shape interface and classes for Rectangle, Circle, and Triangle that implement the Shape interface to calculate area and perimeter.

Uploaded by

isayashpbende26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Interface in Java

The document discusses interfaces in Java, including defining an interface, implementing an interface, and using interfaces to achieve multiple inheritance. It provides examples of defining a Shape interface and classes for Rectangle, Circle, and Triangle that implement the Shape interface to calculate area and perimeter.

Uploaded by

isayashpbende26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Interface in JAVA

Outline
 Introduction to Interface
 Multiple Inheritance – Example
 Why Interfaces are needed
 Java's Interface Concept
 Syntax
 Semantic Rules for Interfaces
 Example: An Interface for Shape Classes
 Extending Interface
 Abstract class and Interface
 Benefits of Interfaces
 Java's Most used Interfaces
2
Introduction to Interface
In General, An interface is a deviceor
system that unrelated entities use to interact.
- The English language is an interface between two people.
- A remote control is an interface between you and a television.

3
Introduction to Interface
In Computing, An interface is a shared boundary across
which two or more components of a computer system
exchange information.
- The exchange can be between software, hardware,
humans and combinations of these.

Interface/Medium

4
Introduction to Interface
In Object oriented programming,

- An interface is a common means


for unrelated objects to communicate with each
other.

In Java,interface is a way through which unrelated


- An
objects use to interact with one another.
- Using interface, you can specify what a class must do,
but not how it does it.

- It is not a class but a set of requirements for classes


that implement the interface
5
Multiple Inheritance
- Example
 For a teaching assistant, we want the properties
from both Employee and Student.

Person
Name
displayDetail()

Employee Student
department department
salary() cgpa()

TeachingAssistant

6
Problems with Multiple Inheritance
Consider following declearation:
ta = new TeachingAssistant();
ta.department;
 Name clash problem: Which department does
ta refers to?
 Combinatio problem: Can department from
Student becombined in Teaching
n Employee
and Assistant?
 Selectio problem: Can you select
from Employee between and
n
department from
department
 Replication problem:
Student?
Should departments in there be two
TeachingAssistent? 7
Why Interfaces are needed

• Multiple Inheritance in JAVA is not allowed – cannot


extend more than one class at a time.
• An object may need IS-A relationships with many
type.

Politician Father

Citizen
President

8
Solution for multiple inheritance in
JAVA
public class Person extends Citizen implements Father,
Politician, President {}

public interface Politician public interface President


{ public void {public void winPoll();
joinParty(); }
}

public interface Father {


public class Citizen {
public void care();
}
}

ppuubblilcci cclaal ssssPPeerrs


9
s oonn{{
Java's Interface Concept
• An interface a protocol of behavior as a
collection
defines of method definitions (without
implementation) and constants, that can be
implemented by any class.

• A class that implements the interface agrees to


implement all the methods defined in the interface.

• If a class includes an interface but does not implement


all the methods defined by that interface, then that class
must be declared as abstract.

10
Syntax
The Declaration of Interface consists of a keyword interface,
its name, and the members.
interface InterfaceName {
// constant declaration
static final type variableName = value;
// method declaration
returntype methodname (argumentlist);
}
The Class that Implements Interface called as
Implementation Class uses keyword implements:
class classname implements InterfaceName {
... } 11
Semantic Rules for Interfaces
• Instantiation
Does not make sense on an interface. Interfaces are not
classes. You can never use the new operator to instantiate
an interface.
public interface Comparable {. . . }

Comparable x = new Comparable( );


• Data Type
An interface can be used as a type, like classes. You can
declare interface variables
class Employee implements Comparable {. . . }
Comparable x = new Employee( );
12
Semantic Rules for Interfaces
• Access modifiers

An interface can be public or “friendly” (default).

All methods in an interface are by default abstract and public.

- Static, final, private, and protected cannot be used.

All variables (“constants”) are public static final by default

-Private, protected cannot be used.

13
Example: An Interface for Shape
Classes
• Creating classes to represent rectangles,
circles, and triangles and compute
their area and perimeter

It may seem as there is an inheritance relationship


here, because rectangle, circle, and triangle are all
shapes.

But code sharing is not useful in this case


because each shape computes its area and
perimeter in a different way.
14
Define Interface Shape

• A better solution would be to write an interface called


Shape to represent the common functionality (to
compute an area and a perimeter ) of all shapes:

public interface Shape


{ public double
getArea();
public double
getPerimeter();
}

15
Class Rectangle implements
interface Shape
public class Rectangle implements Shape {
private double width;
private double height;

// Constructs a new rectangle with the given


dimensions.
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Returns the area of this rectangle.
public double getArea() {
return width * height;
}
// Returns the perimeter of this rectangle.
public double getPerimeter() {
return 2.0 * (width + height);
}
16
}
Class Circle implements interface
Shape
public class Circle implements Shape {
private double radius;

// Constructs a new circle with the given radius.


public Circle(double radius) {
this.radius = radius;
}

// Returns the area of this circle.


public double getArea() {
return Math.PI * radius * radius;
}

// Returns the perimeter of this circle.


public double getPerimeter() {
return 2.0 * Math.PI * radius;
}
}
17
Class Triangle implements
interface Shape
public class Triangle implements Shape {
private double a;
private double b;
private double c;

// Constructs a new Triangle given side lengths.


public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}

// Returns this triangle's area using Heron's formula.


public double getArea() {
double s = (a + b + c) / 2.0;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
// Returns the perimeter of this triangle.
public double getPerimeter() {
return a + b + c; 18
}}
Class Mensuration with Main
Function
class Mensuration
{
public static void main (String [] arg)
{// Rectangle object
Rectangle r = new Rectangle(10,20); // Rectangle object
System.out.println("Area of Rectangle =" +(r.getArea()));
System.out.println("Perimeter of Rectangle =" +
(r.getPerimeter()));
Circle c = new Circle(10); // Circleobject
System.out.println("Area of Circle =" +
(c.getArea()));
System.out.println("Perimeter of Circle =" +
(c.getPerimeter()));
Triangle t = new Triangle(3,4,5); //
Triangle object
System.out.println("Area of Triangle =" +
(t.getArea()));
}
System.out.println("Perimeter of Triangle =" + 19
}
(t.getPerimeter()));
Output Screen

20
Extending Interface
• One interface can inherit another interface using the
extends keyword and not the implements keyword.
• For example,
interface A extends B { }

• Obviously, any class which implements a “sub-


interface” will have to implement each of the methods
contained in it’s “super-interface” also.

21
Abstract class and Interface
Abstract class Interface
A programmer uses an abstract A programmer writes an interface
class when there are some when all the features have
common features shared by all the different implementations for
objects. different objects.
Multiple inheritance not Multiple inheritance possible (Only
possible (Multiple “parent” one “parent” class)
interfaces)
An abstract class contain both An interface contain only abstract
abstract and concrete(non method
abstract) method
In abstract class, abstract keyword abstract keyword is optional to
is compulsory to declare a method declare a method as an abstract in
as an abstract interface

An abstract class can An interface can have only


have protected, public abstract public abstract method
method
Abstract class contain any type of Interface contain only static
variable final variable (constant) 22
Benefits of Interfaces

• Following concepts of object oriented programming


can be achieved using Interface in JAVA.

1.Abstraction
2.Multiple Inheritance
3.polymorphism

23
Some of Java's Most used
Interfaces
• Iterator
To run through a collection of objects without knowing how
the objects are stored, e.g., array.
• Cloneable
Used to make a copy of an existing object via the clone()
method on the class Object. This interface is empty.
• Serializable
Used to Pack a group of objects such that it can be send
over a network or stored to disk. This interface is empty.
• Comparable
The Comparable interface contains a compareTo method,
and this method must take an Object parameter and
return an integer
24

You might also like