SlideShare a Scribd company logo
JAVA

INTERFACES
Interfaces
What is an Interface?
Creating an Interface
Implementing an Interface
What is Marker Interface?
Inheritance
OOP allows you to derive new classes from existing
classes. This is called inheritance.
Inheritance is an important and powerful concept in
Java. Every class you define in Java is inherited from
an existing class.
 Sometimes it is necessary to derive a subclass from
several classes, thus inheriting their data and methods.
In Java only one parent class is allowed.
With interfaces, you can obtain effect of multiple
inheritance.
What is an Interface?
An interface is a classlike construct that contains only
constants and abstract methods.
Cannot be instantiated. Only classes that implements
interfaces can be instantiated. However, final
public static variables can be defined to interface
types.
Why not just use abstract classes? Java does not permit
multiple inheritance from classes, but permits
implementation of multiple interfaces.
What is an Interface?
Protocol for classes that completely separates
specification/behaviour from implementation.

One class can implement many interfaces

One interface can be implemented by many classes

By providing the interface keyword, Java allows you
to fully utilize the "one interface, multiple methods"
aspect of polymorphism.
Why an Interface?
Normally, in order for a method to be called from one
class to another, both classes need to be present at
compile time so the Java compiler can check to ensure
that the method signatures are compatible.
In a system like this, functionality gets pushed up
higher and higher in the class hierarchy so that the
mechanisms will be available to more and more
subclasses.
Interfaces are designed to avoid this problem.
Interfaces are designed to support dynamic method
resolution at run time.
Why an Interface?
Interfaces are designed to avoid this problem.
They disconnect the definition of a method or set of
methods from the inheritance hierarchy.
Since interfaces are in a different hierarchy from classes,
it is possible for classes that are unrelated in terms of the
class hierarchy to implement the same interface.
This is where the real power of interfaces is realized.
Creating an Interface
File InterfaceName.java:
modifier interface InterfaceName
{
  constants declarations;
  methods signatures;
}
Modifier is public or not used.

File ClassName.java:
modifier Class ClassName implements InterfaceName
{
   methods implementation;
}
If a class implements an interface, it should override all
the abstract methods declared in the interface.
Creating an Interface
public interface MyInterface
{
  public void aMethod1(int i); // an abstract methods
  public void aMethod2(double a);
...
  public void aMethodN();

}

public Class    MyClass implements MyInterface
{
  public void   aMethod1(int i) { // implementaion }
  public void   aMethod2(double a) { // implementaion }
  ...
  public void   aMethodN() { // implementaion }
}
Creating a Multiple Interface
modifier interface InterfaceName1 {
    methods1 signatures;
}
modifier interface InterfaceName2 {
    methods2 signatures;
}
...
modifier interface InterfaceNameN {
    methodsN signatures;
}
Creating a Multiple Interface
If a class implements a multiple interfaces, it should
 override all the abstract methods declared in all the
interfaces.
public Class ClassName implements InterfaceName1,
                InterfaceName2, …, InterfaceNameN
{
  methods1 implementation;
  methods2 implementation;
  ...
  methodsN implementation;
}
Example of Creating an Interface

// This interface is defined in
// java.lang package
public interface Comparable
{
  public int compareTo(Object obj);
}
Comparable Circle
public interface Comparable
{
  public int compareTo(Object obj);
}
public Class Circle extends Shape
                            implements Comparable {
   public int compareTo(Object o) {
      if (getRadius() > ((Circle)o).getRadius())
        return 1;
      else if (getRadius()<((Circle)o).getRadius())
        return -1;
      else
         return 0;
   }
}
Comparable Cylinder
public Class Cylinder extends Circle
                     implements Comparable {
    public int compareTo(Object o) {
      if(findVolume() > ((Cylinder)o).findVolume())
         return 1;
      else if(findVolume()<((Cylinder)o).findVolume())
         return -1;
      else
        return 0;
  }
}
Generic findMax Method
public class Max
{
    // Return the maximum between two objects
    public static Comparable findMax(Comparable ob1,
                                Comparable ob2)
    {
      if (ob1.compareTo(ob2) > 0)
         return ob1;
      else
         return ob2;
    }
}
Access via interface reference
You can declare variables as object references that use an interface
rather than a class type. Any instance of any class that implements
the declared interface can be stored in such a variable.
Example: Comparable circle;
When you call a method through one of these references, the
correct version will be called based on the actual instance of the
interface being referred to.
Example: circle = Max.findMax(circle1, circle2);
Using Interface
This is one of the key features of interfaces.
  The method to be executed is looked up dynamically at
  run time, allowing classes to be created later than the
  code which calls methods on them.
  The calling code can dispatch through an interface
  without having to know anything about the "callee."
Using Interface
Objective: Use the findMax() method to find a
 maximum circle between two circles:
Circle circle1 = new Circle(5);
Circle circle2 = new Circle(10);
Comparable circle = Max.findMax(circle1, circle2);
System.out.println(((Circle)circle).getRadius());
// 10.0
System.out.println(circle.toString());
// [Circle] radius = 10.0
Interfaces vs. Abstract Classes
♦ In an interface, the data must be constants;
 an abstract class can have all types of data.
♦ Each method in an interface has only a
 signature without implementation;
♦ An abstract class can have concrete
 methods. An abstract class must contain at
 least one abstract method or inherit from
 another abstract method.
Interfaces vs. Abstract Classes,
              cont.

Since all the methods defined in an interface
are abstract methods, Java does not require
you to put the abstract modifier in the
methods in an interface, but you must put the
abstract modifier before an abstract
method in an abstract class.
Interfaces & Classes
  Interface1_2                        Interface2_2



  Interface1_1      Interface1        Interface2_1




   Object            Class1                             Class2

• One interface can inherit another by use of the keyword extends.
   The syntax is the same as for inheriting classes.
• When a class implements an interface that inherits another
  interface, it must provide implementations for all methods
  defined within the interface inheritance chain.
Interfaces & Classes, cont.
public Interface1_1 { … }   Interface1_2                Interface2_2


public Interface1_2 { … }   Interface1_1   Interface1   Interface2_1

public Interface2_1 { … }
public Interface2_2 { … }   Object          Class1                     Class2




public Interface1 extends Interface1_1, Interface1_2
{...}

public abstract Class Class1 implements Interface1
{... }

public Class Class2 extends Class1 implements
Interface2_1, Interface2_2 {...}
Interfaces vs. Absract Classes
♦ A strong is-a relationship that clearly describes a parent-child
relationship should be modeled using classes.
 Example: a staff member is a person.

♦ A weak is-a relationship, also known as a-kind-of
relationship, indicates that an object possesses a certain
property.
 Example: all strings are comparable, so the String class
 implements the Comparable interface.

♦ The interface names are usually adjectives. You can also use
interfaces to circumvent single inheritance restriction if multiple
inheritance is desired. You have to design one as a superclass,
and the others as interfaces.
The Cloneable Interfaces
Marker Interface: An empty interface.
A marker interface does not contain constants
or methods, but it has a special meaning to the
Java system. The Java system requires a class
to implement the Cloneable interface to
become cloneable.
public interface Cloneable
{
}
The Cloneable Interfaces
public Class Circle extends Shape
                        implements Cloneable {
  public Object clone() {
    try {
        return super.clone()
    }
    catch (CloneNotSupportedException ex) {
         return null;
    }
  }
  ------------------------------------------
  Circle c1 = new Circle(1,2,3);
  Circle c2 = (Circle)c1.clone();

  This is shallow copying with super.clone() method.
  Override the method for deep copying.

More Related Content

What's hot (20)

Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
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
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
atozknowledge .com
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 

Viewers also liked (6)

Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Java interface
Java interfaceJava interface
Java interface
BHUVIJAYAVELU
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Ad

Similar to Java interfaces (20)

Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
RihabBENLAMINE
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
AsifMulani17
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
VarunP31
 
javainterface
javainterfacejavainterface
javainterface
Arjun Shanka
 
Interfaces
InterfacesInterfaces
Interfaces
Jai Marathe
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
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
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
usmanusman720379
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Interfaces .net
Interfaces .netInterfaces .net
Interfaces .net
ajeela mushtaq
 
Interfaces
InterfacesInterfaces
Interfaces
RBIEBT,MOHALI
 
Interfaces
InterfacesInterfaces
Interfaces
myrajendra
 
Java interface
Java interfaceJava interface
Java interface
GaneshKumarKanthiah
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
Niit Care
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
RihabBENLAMINE
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
VarunP31
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Ad

More from Raja Sekhar (8)

Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
Raja Sekhar
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
Raja Sekhar
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
Raja Sekhar
 
Java Starting
Java StartingJava Starting
Java Starting
Raja Sekhar
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
Raja Sekhar
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
Raja Sekhar
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
Raja Sekhar
 

Recently uploaded (20)

Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Trends Report: Artificial Intelligence (AI)
Trends Report: Artificial Intelligence (AI)Trends Report: Artificial Intelligence (AI)
Trends Report: Artificial Intelligence (AI)
Brian Ahier
 
The case for on-premises AI
The case for on-premises AIThe case for on-premises AI
The case for on-premises AI
Principled Technologies
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Trends Report: Artificial Intelligence (AI)
Trends Report: Artificial Intelligence (AI)Trends Report: Artificial Intelligence (AI)
Trends Report: Artificial Intelligence (AI)
Brian Ahier
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 

Java interfaces

  • 2. Interfaces What is an Interface? Creating an Interface Implementing an Interface What is Marker Interface?
  • 3. Inheritance OOP allows you to derive new classes from existing classes. This is called inheritance. Inheritance is an important and powerful concept in Java. Every class you define in Java is inherited from an existing class. Sometimes it is necessary to derive a subclass from several classes, thus inheriting their data and methods. In Java only one parent class is allowed. With interfaces, you can obtain effect of multiple inheritance.
  • 4. What is an Interface? An interface is a classlike construct that contains only constants and abstract methods. Cannot be instantiated. Only classes that implements interfaces can be instantiated. However, final public static variables can be defined to interface types. Why not just use abstract classes? Java does not permit multiple inheritance from classes, but permits implementation of multiple interfaces.
  • 5. What is an Interface? Protocol for classes that completely separates specification/behaviour from implementation. One class can implement many interfaces One interface can be implemented by many classes By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism.
  • 6. Why an Interface? Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. In a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses. Interfaces are designed to avoid this problem. Interfaces are designed to support dynamic method resolution at run time.
  • 7. Why an Interface? Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface. This is where the real power of interfaces is realized.
  • 8. Creating an Interface File InterfaceName.java: modifier interface InterfaceName { constants declarations; methods signatures; } Modifier is public or not used. File ClassName.java: modifier Class ClassName implements InterfaceName { methods implementation; } If a class implements an interface, it should override all the abstract methods declared in the interface.
  • 9. Creating an Interface public interface MyInterface { public void aMethod1(int i); // an abstract methods public void aMethod2(double a); ... public void aMethodN(); } public Class MyClass implements MyInterface { public void aMethod1(int i) { // implementaion } public void aMethod2(double a) { // implementaion } ... public void aMethodN() { // implementaion } }
  • 10. Creating a Multiple Interface modifier interface InterfaceName1 { methods1 signatures; } modifier interface InterfaceName2 { methods2 signatures; } ... modifier interface InterfaceNameN { methodsN signatures; }
  • 11. Creating a Multiple Interface If a class implements a multiple interfaces, it should override all the abstract methods declared in all the interfaces. public Class ClassName implements InterfaceName1, InterfaceName2, …, InterfaceNameN { methods1 implementation; methods2 implementation; ... methodsN implementation; }
  • 12. Example of Creating an Interface // This interface is defined in // java.lang package public interface Comparable { public int compareTo(Object obj); }
  • 13. Comparable Circle public interface Comparable { public int compareTo(Object obj); } public Class Circle extends Shape implements Comparable { public int compareTo(Object o) { if (getRadius() > ((Circle)o).getRadius()) return 1; else if (getRadius()<((Circle)o).getRadius()) return -1; else return 0; } }
  • 14. Comparable Cylinder public Class Cylinder extends Circle implements Comparable { public int compareTo(Object o) { if(findVolume() > ((Cylinder)o).findVolume()) return 1; else if(findVolume()<((Cylinder)o).findVolume()) return -1; else return 0; } }
  • 15. Generic findMax Method public class Max { // Return the maximum between two objects public static Comparable findMax(Comparable ob1, Comparable ob2) { if (ob1.compareTo(ob2) > 0) return ob1; else return ob2; } }
  • 16. Access via interface reference You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be stored in such a variable. Example: Comparable circle; When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. Example: circle = Max.findMax(circle1, circle2);
  • 17. Using Interface This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the "callee."
  • 18. Using Interface Objective: Use the findMax() method to find a maximum circle between two circles: Circle circle1 = new Circle(5); Circle circle2 = new Circle(10); Comparable circle = Max.findMax(circle1, circle2); System.out.println(((Circle)circle).getRadius()); // 10.0 System.out.println(circle.toString()); // [Circle] radius = 10.0
  • 19. Interfaces vs. Abstract Classes ♦ In an interface, the data must be constants; an abstract class can have all types of data. ♦ Each method in an interface has only a signature without implementation; ♦ An abstract class can have concrete methods. An abstract class must contain at least one abstract method or inherit from another abstract method.
  • 20. Interfaces vs. Abstract Classes, cont. Since all the methods defined in an interface are abstract methods, Java does not require you to put the abstract modifier in the methods in an interface, but you must put the abstract modifier before an abstract method in an abstract class.
  • 21. Interfaces & Classes Interface1_2 Interface2_2 Interface1_1 Interface1 Interface2_1 Object Class1 Class2 • One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. • When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.
  • 22. Interfaces & Classes, cont. public Interface1_1 { … } Interface1_2 Interface2_2 public Interface1_2 { … } Interface1_1 Interface1 Interface2_1 public Interface2_1 { … } public Interface2_2 { … } Object Class1 Class2 public Interface1 extends Interface1_1, Interface1_2 {...} public abstract Class Class1 implements Interface1 {... } public Class Class2 extends Class1 implements Interface2_1, Interface2_2 {...}
  • 23. Interfaces vs. Absract Classes ♦ A strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes. Example: a staff member is a person. ♦ A weak is-a relationship, also known as a-kind-of relationship, indicates that an object possesses a certain property. Example: all strings are comparable, so the String class implements the Comparable interface. ♦ The interface names are usually adjectives. You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. You have to design one as a superclass, and the others as interfaces.
  • 24. The Cloneable Interfaces Marker Interface: An empty interface. A marker interface does not contain constants or methods, but it has a special meaning to the Java system. The Java system requires a class to implement the Cloneable interface to become cloneable. public interface Cloneable { }
  • 25. The Cloneable Interfaces public Class Circle extends Shape implements Cloneable { public Object clone() { try { return super.clone() } catch (CloneNotSupportedException ex) { return null; } } ------------------------------------------ Circle c1 = new Circle(1,2,3); Circle c2 = (Circle)c1.clone(); This is shallow copying with super.clone() method. Override the method for deep copying.