SlideShare a Scribd company logo
JAVA
Interface
Prepared by
Miss. Arati A. Gadgil
Multiple inheritance means creating a new class that inherits
behavior directly from more than one superclass.
Java does not support multiple inheritance reason is
ambiguity around Diamond problem, consider a class A
has show() method and then B and C derived from A and has
there own show() implementation and now class D derive
from B and C using multiple inheritance and if we refer
just show() compiler will not be able to decide which show() it
should invoke. This is also called Diamond problem because
structure on this inheritance scenario is similar to 4 edge
diamond.
3
A
Show()
B
Show()
C
Show()
D
Show()
4
Interface is Like a class but only contains abstract method and
final variables
example:
interface Operation{
void Add(int a,intnt b);
int Sub(int a,int b);
}
abstract interface Operation{
public abstract sub();
public abstract int sub();
}
Both are correct!
the abstract and public
keywords are implied,
so the shorthand is
recommended.
5
An interface is very much like a class-with one important
difference. None of the methods declared in an
interface are implemented in the interface itself. Instead, these
methods must be implemented in any class that uses the
interface.
In short, interfaces describe behaviors but do not detail how
those behaviors will be carried out.
We save the interface's source code in a file with the .java
extension. Then use the Java compiler, javac, to compile the
source code into byte-code form. Just like a normal class, the
byte-code file will have the .class extension.
6
Difference between class and interface
we cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface is not extended by a class; it is implemented by
a class.
An interface can extend multiple interfaces.
7
interface student
{
public void add();
public void display();
}
For defining interface use interface keyword followed by
interface name as shown above.
When any class implements the interface then, class must be
write the definition of all methods in the interface.
8
class A implements student
{
String nm;
public void add()
{ Scanner sc=new Scanner(System.in);
nm=sc.nextLine();
}
public void display()
{
System.out.print("Student name="+nm);
}
}
9
Extending interface
An interface can extend another interface, similarly to the way
that a class can extend another class.
The extends keyword is used to extend an interface, and the
child interface inherits the methods of the parent interface.
10
interface abc
{
static int a=10;
void displayA();
}
interface student extends abc
{
void add();
void display();
}
11
Using an Interface as a Type
When you define a new interface, you are defining a new
reference data type. You can use interface names anywhere
you can use any other data type name.
If you define a reference variable whose type is an interface,
any object you assign to it must be an instance of a class that
implements the interface.
As an example,method for finding the largest object in a pair
of objects, for any objects that are instantiated from a class
that implements Relatable
12
public Object findLargest(Object object1, Object object2)
{
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ((obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
}
By casting object1 to a Relatable type, it can invoke
the isLargerThan method
13
Evolving Interfaces
Consider an interface that we have developed called operation:
public interface opertion
{
void Add(int i, int j);
void Mul(int a,int b);
}
Suppose later we want to add a new method so now interface
will be
public interface opertion
{
void Add(int i, int j);
void Mul(int a, int b);
void Sub(int c, int d);
}
14
If we make this change, then all classes that implement the
old operation interface will break because they no longer
implement the old interface.
 If you want to add additional methods to an interface, you
have several options. Create oprationplus interface that
extends operation:
public interface operationplus extends operation
{
void sub(int c, int d);
}
15
Default methods
Alternatively, we can define your new methods as default
methods. The following example defines a default method
named sub:
public interface opertion
{
void Add(int i, int j);
void Mul(int a, int b);
default void Sub(int c, int d){//method body}
}
Note that we must provide an implementation for default
methods
Thank You
16

More Related Content

What's hot

Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Interface in java
Interface in javaInterface in java
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Interface java
Interface java Interface java
Interface java
atiafyrose
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
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
CPD INDIA
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
Oum Saokosal
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
Arindam Ghosh
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
Edureka!
 

What's hot (20)

Java interface
Java interfaceJava interface
Java interface
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Interface in java
Interface in javaInterface in java
Interface in java
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
javainterface
javainterfacejavainterface
javainterface
 
Interface java
Interface java Interface java
Interface java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
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
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 

Similar to Java interface

Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
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
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
Naga Muruga
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
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
rani marri
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
Abou Bakr Ashraf
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
Nipam Medhi
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
AsifMulani17
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
VarunP31
 
Java interface
Java interface Java interface
Java interface
HoneyChintal
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
KumarUtsav24
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
Richa Gupta
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
vivek p s
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
dashpayal697
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
 

Similar to Java interface (20)

Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Interface in java
Interface in javaInterface in java
Interface in java
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
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
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
Interfaces
InterfacesInterfaces
Interfaces
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Java interface
Java interface Java interface
Java interface
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 

More from Arati Gadgil

Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
 
Java swing
Java swingJava swing
Java swing
Arati Gadgil
 
Java applet
Java appletJava applet
Java applet
Arati Gadgil
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
Arati Gadgil
 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
 
Java stream
Java streamJava stream
Java stream
Arati Gadgil
 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
 
Java networking
Java networkingJava networking
Java networking
Arati Gadgil
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Java package
Java packageJava package
Java package
Arati Gadgil
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
Arati Gadgil
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Java collection
Java collectionJava collection
Java collection
Arati Gadgil
 
Java class
Java classJava class
Java class
Arati Gadgil
 
Java basic
Java basicJava basic
Java basic
Arati Gadgil
 

More from Arati Gadgil (16)

Java adapter
Java adapterJava adapter
Java adapter
 
Java swing
Java swingJava swing
Java swing
 
Java applet
Java appletJava applet
Java applet
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Java awt
Java awtJava awt
Java awt
 
Java stream
Java streamJava stream
Java stream
 
Java thread
Java threadJava thread
Java thread
 
Java networking
Java networkingJava networking
Java networking
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Java package
Java packageJava package
Java package
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
 
Java exception
Java exception Java exception
Java exception
 
Java collection
Java collectionJava collection
Java collection
 
Java class
Java classJava class
Java class
 
Java basic
Java basicJava basic
Java basic
 

Recently uploaded

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 

Recently uploaded (20)

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 

Java interface

  • 2. Multiple inheritance means creating a new class that inherits behavior directly from more than one superclass. Java does not support multiple inheritance reason is ambiguity around Diamond problem, consider a class A has show() method and then B and C derived from A and has there own show() implementation and now class D derive from B and C using multiple inheritance and if we refer just show() compiler will not be able to decide which show() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond.
  • 4. 4 Interface is Like a class but only contains abstract method and final variables example: interface Operation{ void Add(int a,intnt b); int Sub(int a,int b); } abstract interface Operation{ public abstract sub(); public abstract int sub(); } Both are correct! the abstract and public keywords are implied, so the shorthand is recommended.
  • 5. 5 An interface is very much like a class-with one important difference. None of the methods declared in an interface are implemented in the interface itself. Instead, these methods must be implemented in any class that uses the interface. In short, interfaces describe behaviors but do not detail how those behaviors will be carried out. We save the interface's source code in a file with the .java extension. Then use the Java compiler, javac, to compile the source code into byte-code form. Just like a normal class, the byte-code file will have the .class extension.
  • 6. 6 Difference between class and interface we cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.
  • 7. 7 interface student { public void add(); public void display(); } For defining interface use interface keyword followed by interface name as shown above. When any class implements the interface then, class must be write the definition of all methods in the interface.
  • 8. 8 class A implements student { String nm; public void add() { Scanner sc=new Scanner(System.in); nm=sc.nextLine(); } public void display() { System.out.print("Student name="+nm); } }
  • 9. 9 Extending interface An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
  • 10. 10 interface abc { static int a=10; void displayA(); } interface student extends abc { void add(); void display(); }
  • 11. 11 Using an Interface as a Type When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface. As an example,method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable
  • 12. 12 public Object findLargest(Object object1, Object object2) { Relatable obj1 = (Relatable)object1; Relatable obj2 = (Relatable)object2; if ((obj1).isLargerThan(obj2) > 0) return object1; else return object2; } By casting object1 to a Relatable type, it can invoke the isLargerThan method
  • 13. 13 Evolving Interfaces Consider an interface that we have developed called operation: public interface opertion { void Add(int i, int j); void Mul(int a,int b); } Suppose later we want to add a new method so now interface will be public interface opertion { void Add(int i, int j); void Mul(int a, int b); void Sub(int c, int d); }
  • 14. 14 If we make this change, then all classes that implement the old operation interface will break because they no longer implement the old interface.  If you want to add additional methods to an interface, you have several options. Create oprationplus interface that extends operation: public interface operationplus extends operation { void sub(int c, int d); }
  • 15. 15 Default methods Alternatively, we can define your new methods as default methods. The following example defines a default method named sub: public interface opertion { void Add(int i, int j); void Mul(int a, int b); default void Sub(int c, int d){//method body} } Note that we must provide an implementation for default methods