SlideShare a Scribd company logo
Java Interfaces
Sujit Kumar
Zenolocity LLC
What is an Interface?
• It is a contract that declares a set of abilities (WHAT)
without defining how it is implemented.
• Any concrete class that implements this interface is
signing up to implement all the methods specified in
the contract (interface).
Example:
public interface Automobile {
public abstract start();
public abstract accelerate();
public abstract stop();
}
Features of an Interface
• By default, all methods are public and abstract
(no body).
• No instance variables.
• Any variable defined in an interface is
public, static and final. (implies class level
constants).
• A concrete class implements all methods of an
interface.
Example:
public class Triangle implements Shape {
// All methods of Shape have to be implemented.
}
Abstract Class implement Interface
• An abstract class can implement some
methods of an interface. It needs to be left
with at least 1 unimplemented method.
Example:
public abstract class TwoDimShape
implements Shape {
// Some methods of Shape have to be implemented.
}
Interfaces solve Multiple Inheritance
problem in Java
• A class cannot extend more than 1 class.
• A class can implement multiple interfaces at
the same time.
• An interface can also extend multiple
interfaces.
• A java class can extend exactly 1 class and
implement multiple interfaces at the same
time.
Polymorphism via Interfaces
• A class implementing an interface has an
“is-a” relationship with it’s interface.
• If Triangle class implements the Shape
class, then every triangle is a shape.
• Hence, we can say: Shape s = new Triangle();
• Also, if a method parameter expects an
interface, you can invoke that method by
passing a class object that implements that
interface.
Program to Interfaces
• Instead of writing your classes in a way that says:
I depend on this specific class to do this stuff
• you write it in a way that says
I depend on any interface that does this stuff
Examples:
Drivable[] myDrivables = new Drivable[2];
myDrivables[0] = new Car();
myDrivables[1] = new Bike();
for (int i = 0; i < 2; ++i)
{
myDrivables[i].accelerate();
}
Good to Know: Liskov Substitution
Principle
• In a computer program, if S is a subtype of
T, then objects of type S may be
substituted for objects of type T without
altering the correctness of the program.
Difference between Abstract Classes
and Interfaces
Feature

Abstract Class

Interface

Abstract Methods

At least ONE

All are abstract

Inheritance

Can extend only ONE

Interface CAN extend many
Class CAN implement many

Instance Variables

CAN have it

CANNOT have any

Access modifiers

public, private or protected ONLY public

Constructors

CAN have it

CANNOT have any
Abstract Classes or Interfaces ?
• If design can potentially change in the future, use
interfaces.
• If inheritance hierarchy is stable, use abstract
classes, especially for all non-leaf classes in the
hierarchy.
• If various implementations only share method
signatures, use interfaces.
• If various implementations share both method
signatures & state, use abstract classes.
• If only some implementations need to provide a
certain behavior, use interfaces.

More Related Content

What's hot (15)

Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 
Abstract method
Abstract methodAbstract method
Abstract method
Yaswanth Babu Gummadivelli
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
 
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!
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
Linh Lê
 
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
Simplilearn
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Abstract class
Abstract classAbstract class
Abstract class
Hoang Nguyen
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
Mazharul Sabbir
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
 
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!
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
Linh Lê
 
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
Simplilearn
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
Mazharul Sabbir
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 

Viewers also liked (10)

Debug a java program
Debug a java programDebug a java program
Debug a java program
Sujit Kumar
 
Java dates
Java datesJava dates
Java dates
Sujit Kumar
 
Java enum
Java enumJava enum
Java enum
Sujit Kumar
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classes
Sujit Kumar
 
Java polymorphism
Java polymorphismJava polymorphism
Java polymorphism
Sujit Kumar
 
Java final keyword
Java final keywordJava final keyword
Java final keyword
Sujit Kumar
 
Java Web Development Course
Java Web Development CourseJava Web Development Course
Java Web Development Course
Sujit Kumar
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
Sujit Kumar
 
Java file paths
Java file pathsJava file paths
Java file paths
Sujit Kumar
 
Debug a java program
Debug a java programDebug a java program
Debug a java program
Sujit Kumar
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classes
Sujit Kumar
 
Java polymorphism
Java polymorphismJava polymorphism
Java polymorphism
Sujit Kumar
 
Java final keyword
Java final keywordJava final keyword
Java final keyword
Sujit Kumar
 
Java Web Development Course
Java Web Development CourseJava Web Development Course
Java Web Development Course
Sujit Kumar
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
Sujit Kumar
 

Similar to Java interfaces (20)

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
Jamsher bhanbhro
 
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
mdmahamudul894
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
usmanusman720379
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
Sireesh K
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Abstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphismAbstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
14 interface
14  interface14  interface
14 interface
Ravindra Rathore
 
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
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
rajshreemuthiah
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Abstract Classes and Interfaces in oop.pptx
Abstract Classes and Interfaces in oop.pptxAbstract Classes and Interfaces in oop.pptx
Abstract Classes and Interfaces in oop.pptx
haiderkhooradnan
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
SudhanshiBakre1
 
Oop
OopOop
Oop
MD.ANISUR RAHMAN
 
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptxabstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
haiderkhooradnan
 
Interface
InterfaceInterface
Interface
Muthiah Abbhirami
 
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
Jamsher bhanbhro
 
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
8.-Abstract-Class-and-Interfaces.pdf vk sir.pdf
mdmahamudul894
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
Sireesh K
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Abstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphismAbstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
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
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
rajshreemuthiah
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Abstract Classes and Interfaces in oop.pptx
Abstract Classes and Interfaces in oop.pptxAbstract Classes and Interfaces in oop.pptx
Abstract Classes and Interfaces in oop.pptx
haiderkhooradnan
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
SudhanshiBakre1
 
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptxabstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
haiderkhooradnan
 

More from Sujit Kumar (20)

SFDC Database Basics
SFDC Database BasicsSFDC Database Basics
SFDC Database Basics
Sujit Kumar
 
SFDC Database Security
SFDC Database SecuritySFDC Database Security
SFDC Database Security
Sujit Kumar
 
SFDC Social Applications
SFDC Social ApplicationsSFDC Social Applications
SFDC Social Applications
Sujit Kumar
 
SFDC Other Platform Features
SFDC Other Platform FeaturesSFDC Other Platform Features
SFDC Other Platform Features
Sujit Kumar
 
SFDC Outbound Integrations
SFDC Outbound IntegrationsSFDC Outbound Integrations
SFDC Outbound Integrations
Sujit Kumar
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
Sujit Kumar
 
SFDC UI - Advanced Visualforce
SFDC UI - Advanced VisualforceSFDC UI - Advanced Visualforce
SFDC UI - Advanced Visualforce
Sujit Kumar
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to Visualforce
Sujit Kumar
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC Deployments
Sujit Kumar
 
SFDC Batch Apex
SFDC Batch ApexSFDC Batch Apex
SFDC Batch Apex
Sujit Kumar
 
SFDC Data Loader
SFDC Data LoaderSFDC Data Loader
SFDC Data Loader
Sujit Kumar
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex
Sujit Kumar
 
SFDC Introduction to Apex
SFDC Introduction to ApexSFDC Introduction to Apex
SFDC Introduction to Apex
Sujit Kumar
 
SFDC Database Additional Features
SFDC Database Additional FeaturesSFDC Database Additional Features
SFDC Database Additional Features
Sujit Kumar
 
Introduction to SalesForce
Introduction to SalesForceIntroduction to SalesForce
Introduction to SalesForce
Sujit Kumar
 
More about java strings - Immutability and String Pool
More about java strings - Immutability and String PoolMore about java strings - Immutability and String Pool
More about java strings - Immutability and String Pool
Sujit Kumar
 
Hibernate First and Second level caches
Hibernate First and Second level cachesHibernate First and Second level caches
Hibernate First and Second level caches
Sujit Kumar
 
Java equals hashCode Contract
Java equals hashCode ContractJava equals hashCode Contract
Java equals hashCode Contract
Sujit Kumar
 
Java Comparable and Comparator
Java Comparable and ComparatorJava Comparable and Comparator
Java Comparable and Comparator
Sujit Kumar
 
Java build tools
Java build toolsJava build tools
Java build tools
Sujit Kumar
 
SFDC Database Basics
SFDC Database BasicsSFDC Database Basics
SFDC Database Basics
Sujit Kumar
 
SFDC Database Security
SFDC Database SecuritySFDC Database Security
SFDC Database Security
Sujit Kumar
 
SFDC Social Applications
SFDC Social ApplicationsSFDC Social Applications
SFDC Social Applications
Sujit Kumar
 
SFDC Other Platform Features
SFDC Other Platform FeaturesSFDC Other Platform Features
SFDC Other Platform Features
Sujit Kumar
 
SFDC Outbound Integrations
SFDC Outbound IntegrationsSFDC Outbound Integrations
SFDC Outbound Integrations
Sujit Kumar
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
Sujit Kumar
 
SFDC UI - Advanced Visualforce
SFDC UI - Advanced VisualforceSFDC UI - Advanced Visualforce
SFDC UI - Advanced Visualforce
Sujit Kumar
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to Visualforce
Sujit Kumar
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC Deployments
Sujit Kumar
 
SFDC Data Loader
SFDC Data LoaderSFDC Data Loader
SFDC Data Loader
Sujit Kumar
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex
Sujit Kumar
 
SFDC Introduction to Apex
SFDC Introduction to ApexSFDC Introduction to Apex
SFDC Introduction to Apex
Sujit Kumar
 
SFDC Database Additional Features
SFDC Database Additional FeaturesSFDC Database Additional Features
SFDC Database Additional Features
Sujit Kumar
 
Introduction to SalesForce
Introduction to SalesForceIntroduction to SalesForce
Introduction to SalesForce
Sujit Kumar
 
More about java strings - Immutability and String Pool
More about java strings - Immutability and String PoolMore about java strings - Immutability and String Pool
More about java strings - Immutability and String Pool
Sujit Kumar
 
Hibernate First and Second level caches
Hibernate First and Second level cachesHibernate First and Second level caches
Hibernate First and Second level caches
Sujit Kumar
 
Java equals hashCode Contract
Java equals hashCode ContractJava equals hashCode Contract
Java equals hashCode Contract
Sujit Kumar
 
Java Comparable and Comparator
Java Comparable and ComparatorJava Comparable and Comparator
Java Comparable and Comparator
Sujit Kumar
 
Java build tools
Java build toolsJava build tools
Java build tools
Sujit Kumar
 

Recently uploaded (20)

IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 

Java interfaces

  • 2. What is an Interface? • It is a contract that declares a set of abilities (WHAT) without defining how it is implemented. • Any concrete class that implements this interface is signing up to implement all the methods specified in the contract (interface). Example: public interface Automobile { public abstract start(); public abstract accelerate(); public abstract stop(); }
  • 3. Features of an Interface • By default, all methods are public and abstract (no body). • No instance variables. • Any variable defined in an interface is public, static and final. (implies class level constants). • A concrete class implements all methods of an interface. Example: public class Triangle implements Shape { // All methods of Shape have to be implemented. }
  • 4. Abstract Class implement Interface • An abstract class can implement some methods of an interface. It needs to be left with at least 1 unimplemented method. Example: public abstract class TwoDimShape implements Shape { // Some methods of Shape have to be implemented. }
  • 5. Interfaces solve Multiple Inheritance problem in Java • A class cannot extend more than 1 class. • A class can implement multiple interfaces at the same time. • An interface can also extend multiple interfaces. • A java class can extend exactly 1 class and implement multiple interfaces at the same time.
  • 6. Polymorphism via Interfaces • A class implementing an interface has an “is-a” relationship with it’s interface. • If Triangle class implements the Shape class, then every triangle is a shape. • Hence, we can say: Shape s = new Triangle(); • Also, if a method parameter expects an interface, you can invoke that method by passing a class object that implements that interface.
  • 7. Program to Interfaces • Instead of writing your classes in a way that says: I depend on this specific class to do this stuff • you write it in a way that says I depend on any interface that does this stuff Examples: Drivable[] myDrivables = new Drivable[2]; myDrivables[0] = new Car(); myDrivables[1] = new Bike(); for (int i = 0; i < 2; ++i) { myDrivables[i].accelerate(); }
  • 8. Good to Know: Liskov Substitution Principle • In a computer program, if S is a subtype of T, then objects of type S may be substituted for objects of type T without altering the correctness of the program.
  • 9. Difference between Abstract Classes and Interfaces Feature Abstract Class Interface Abstract Methods At least ONE All are abstract Inheritance Can extend only ONE Interface CAN extend many Class CAN implement many Instance Variables CAN have it CANNOT have any Access modifiers public, private or protected ONLY public Constructors CAN have it CANNOT have any
  • 10. Abstract Classes or Interfaces ? • If design can potentially change in the future, use interfaces. • If inheritance hierarchy is stable, use abstract classes, especially for all non-leaf classes in the hierarchy. • If various implementations only share method signatures, use interfaces. • If various implementations share both method signatures & state, use abstract classes. • If only some implementations need to provide a certain behavior, use interfaces.
  翻译: