SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Interface in JAVAInterface in JAVA
Presented By :Presented By :
Mr. Dheeraj Kumar Singh
Assistant Professor
I.T. Department,
Faculty of Engg., Parul University,
Vadodara, Gujarat
OutlineOutline
 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
IntroductionIntroduction toto InterfaceInterface
3
In General, An interface is a device or 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.
IntroductionIntroduction toto InterfaceInterface
4
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
5
In Object oriented programming,
- An interface is a common means for
unrelated objects to communicate with each other.
In Java,
- An interface is a way through which unrelated 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
IntroductionIntroduction toto InterfaceInterface
Multiple InheritanceMultiple Inheritance
- Example- Example
department
cgpa()
Student
department
salary()
Employee
Person
Name
displayDetail()
TeachingAssistantTeachingAssistant
 For a teaching assistant, we want the properties
from both Employee and Student.
6
Problems with Multiple InheritanceProblems with Multiple Inheritance
Consider following declearation:
ta = new TeachingAssistant();
ta.department;
 Name clash problem: Which department does ta
refers to?
Combination problem: Can department from
Employee and Student be combined in Teaching
Assistant?
Selection problem: Can you select between
department from Employee and department from
Student?
Replication problem: Should there be two
departments in TeachingAssistent?
7
Why Interfaces are neededWhy 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
Citizen
Father
President
8
Solution for multiple inheritance inSolution for multiple inheritance in
JAVAJAVA
public class Person extends Citizen implements Father,
Politician, President {}
public interface Politician {
public void joinParty();
}
public class Citizen {
}
public interface Father {
public void care();
}
public interface President
{public void winPoll();
}
public class Person {
}
public class Person {
} 9
10
• An interface defines a protocol of behavior as a
collection 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.
Java's Interface Concept
SyntaxSyntax
The Declaration of Interface consists of a keyword interfaceinterface,
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 implementsimplements:
class classname implementsimplements InterfaceName {
... } 11
12
• 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( );
Semantic Rules for InterfacesSemantic Rules for Interfaces
13
• 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.
Semantic Rules for InterfacesSemantic Rules for Interfaces
14
Example: An Interface for ShapeExample: An Interface for Shape
ClassesClasses
• 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.
15
Define Interface ShapeDefine 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();
}
16
Class Rectangle implementsClass Rectangle implements
interface Shapeinterface 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);
}
}
17
Class Circle implements interfaceClass Circle implements interface
ShapeShape
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;
}
}
18
ClassClass TriangleTriangle implementsimplements
interface Shapeinterface 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;
}}
Class Mensuration with MainClass Mensuration with Main
FunctionFunction
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 =" +
(t.getPerimeter()));
}
}
19
Output ScreenOutput Screen
20
Extending InterfaceExtending 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 InterfaceAbstract class and Interface
Abstract class Interface
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.
Multiple inheritance not possible
(Multiple “parent” interfaces)
Multiple inheritance possible (Only
one “parent” class)
An abstract class contain both
abstract and concrete(non
abstract) method
An interface contain only abstract
method
In abstract class, abstract keyword
is compulsory to declare a method
as an abstract
abstract keyword is optional to
declare a method as an abstract in
interface
An abstract class can have
protected, public abstract method
An interface can have only public
abstract method
Abstract class contain any type of
variable
Interface contain only static final
variable (constant)
22
23
Benefits of InterfacesBenefits of Interfaces
• Following concepts of object oriented programming
can be achieved using Interface in JAVA.
1.Abstraction
2.Multiple Inheritance
3.polymorphism
Some of Java's Most usedSome of Java's Most used
InterfacesInterfaces
• 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
25
Follow me on : https://www.facebook.com/dks.indhttps://www.facebook.com/dks.ind
Email : dhirajsingh66@gmail.comdhirajsingh66@gmail.com
: dheeraj.singh@paruluniversity.ac.in: dheeraj.singh@paruluniversity.ac.in

Weitere ähnliche Inhalte

Was ist angesagt?

Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
Enumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | EdurekaEnumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | EdurekaEdureka!
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 

Was ist angesagt? (20)

Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
OOP java
OOP javaOOP java
OOP java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Java package
Java packageJava package
Java package
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Enumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | EdurekaEnumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | Edureka
 
Data types in java
Data types in javaData types in java
Data types in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 

Ähnlich wie Interface in java By Dheeraj Kumar Singh

oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxssuser84e52e
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdfAdilAijaz3
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#Sireesh K
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 

Ähnlich wie Interface in java By Dheeraj Kumar Singh (20)

Lecture 18
Lecture 18Lecture 18
Lecture 18
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java interface
Java interface Java interface
Java interface
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
 
JAVA.pptx
JAVA.pptxJAVA.pptx
JAVA.pptx
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
javainterface
javainterfacejavainterface
javainterface
 

Kürzlich hochgeladen

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Interface in java By Dheeraj Kumar Singh

  • 1. Interface in JAVAInterface in JAVA Presented By :Presented By : Mr. Dheeraj Kumar Singh Assistant Professor I.T. Department, Faculty of Engg., Parul University, Vadodara, Gujarat
  • 2. OutlineOutline  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
  • 3. IntroductionIntroduction toto InterfaceInterface 3 In General, An interface is a device or 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.
  • 4. IntroductionIntroduction toto InterfaceInterface 4 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
  • 5. 5 In Object oriented programming, - An interface is a common means for unrelated objects to communicate with each other. In Java, - An interface is a way through which unrelated 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 IntroductionIntroduction toto InterfaceInterface
  • 6. Multiple InheritanceMultiple Inheritance - Example- Example department cgpa() Student department salary() Employee Person Name displayDetail() TeachingAssistantTeachingAssistant  For a teaching assistant, we want the properties from both Employee and Student. 6
  • 7. Problems with Multiple InheritanceProblems with Multiple Inheritance Consider following declearation: ta = new TeachingAssistant(); ta.department;  Name clash problem: Which department does ta refers to? Combination problem: Can department from Employee and Student be combined in Teaching Assistant? Selection problem: Can you select between department from Employee and department from Student? Replication problem: Should there be two departments in TeachingAssistent? 7
  • 8. Why Interfaces are neededWhy 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 Citizen Father President 8
  • 9. Solution for multiple inheritance inSolution for multiple inheritance in JAVAJAVA public class Person extends Citizen implements Father, Politician, President {} public interface Politician { public void joinParty(); } public class Citizen { } public interface Father { public void care(); } public interface President {public void winPoll(); } public class Person { } public class Person { } 9
  • 10. 10 • An interface defines a protocol of behavior as a collection 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. Java's Interface Concept
  • 11. SyntaxSyntax The Declaration of Interface consists of a keyword interfaceinterface, 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 implementsimplements: class classname implementsimplements InterfaceName { ... } 11
  • 12. 12 • 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( ); Semantic Rules for InterfacesSemantic Rules for Interfaces
  • 13. 13 • 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. Semantic Rules for InterfacesSemantic Rules for Interfaces
  • 14. 14 Example: An Interface for ShapeExample: An Interface for Shape ClassesClasses • 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.
  • 15. 15 Define Interface ShapeDefine 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(); }
  • 16. 16 Class Rectangle implementsClass Rectangle implements interface Shapeinterface 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); } }
  • 17. 17 Class Circle implements interfaceClass Circle implements interface ShapeShape 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; } }
  • 18. 18 ClassClass TriangleTriangle implementsimplements interface Shapeinterface 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; }}
  • 19. Class Mensuration with MainClass Mensuration with Main FunctionFunction 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 =" + (t.getPerimeter())); } } 19
  • 21. Extending InterfaceExtending 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
  • 22. Abstract class and InterfaceAbstract class and Interface Abstract class Interface 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. Multiple inheritance not possible (Multiple “parent” interfaces) Multiple inheritance possible (Only one “parent” class) An abstract class contain both abstract and concrete(non abstract) method An interface contain only abstract method In abstract class, abstract keyword is compulsory to declare a method as an abstract abstract keyword is optional to declare a method as an abstract in interface An abstract class can have protected, public abstract method An interface can have only public abstract method Abstract class contain any type of variable Interface contain only static final variable (constant) 22
  • 23. 23 Benefits of InterfacesBenefits of Interfaces • Following concepts of object oriented programming can be achieved using Interface in JAVA. 1.Abstraction 2.Multiple Inheritance 3.polymorphism
  • 24. Some of Java's Most usedSome of Java's Most used InterfacesInterfaces • 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
  • 25. 25 Follow me on : https://www.facebook.com/dks.indhttps://www.facebook.com/dks.ind Email : dhirajsingh66@gmail.comdhirajsingh66@gmail.com : dheeraj.singh@paruluniversity.ac.in: dheeraj.singh@paruluniversity.ac.in