SlideShare une entreprise Scribd logo
1  sur  11
Interfaces DhrubojyotiKayal
Some classes are better of just defining common behavior You also want to prevent possibility of creating instances of this class Make the class abstract public class Shape{ 	public abstract void draw()  { 	} } Abstract class
A class containing abstract methods is called an abstract class.  If a class contains one or more abstract methods, the class itself must be qualified as abstract  public abstract class Shape { 	public abstract void draw(); } Abstract Class
You cannot create an object of a class declared abstract If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class  If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword.  It’s possible to make a class abstract without including any abstract methods  You can have methods in an abstract class which have reusable code and they themselves are not declared abstract Abstract Class
Create an abstract Java class with one abstract method and another non abstract method Try to create an instance of the abstract class Excerise
The interface keyword produces a completely abstract class, one that provides no implementation at all.  It allows the creator to determine method names, argument lists, and return types, but no method bodies.  An interface provides only a form, but no implementation.  An interface says, "All classes that implement this particular interface will look like this."  Any code that uses a particular interface knows what methods might be called for that interface, and that’s all.  So the interface is used to establish a "protocol" between classes  Interfaces
To create an interface, use the interface keyword instead of the class keyword  Same access specifiers work as with classes, but mostly public An interface can also contain fields, but these are implicitly static and final.  To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword  Interfaces
public interface Singable { 	public void sing(); } public class Parrot implements Singable { 	public void sing() { 	} 	public void fly() { 	} } Interface in Action
Design an interface to ensure that Shape class and its children– Rectangle, Square can be drawn Design an interface to ensure that Shape class and its children – Rectangle, Square can be filled with color Now write a Test class and create two references one each of the children and assign them to a Shape variable. Invoke the different methods  Now try assigning the Shape references to the interface variables. Try invoking the methods  Exercise
Just like class it is possible to extend an interface public interface A { 	public int a(); } public interface B extends a { } Extending an interface
Q&A

Contenu connexe

Tendances

Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalOUM SAOKOSAL
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.Abid Kohistani
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAbid Kohistani
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismEng Teong Cheah
 
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
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
OOP in C#
OOP in C#OOP in C#
OOP in C#DevMix
 

Tendances (16)

Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
 
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...
 
Abstract class
Abstract classAbstract class
Abstract class
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Java interface
Java interface Java interface
Java interface
 
OOP in C#
OOP in C#OOP in C#
OOP in C#
 

En vedette (12)

属灵生命的成长与整合 Apr 29 2012
属灵生命的成长与整合   Apr 29 2012属灵生命的成长与整合   Apr 29 2012
属灵生命的成长与整合 Apr 29 2012
 
Tue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yangTue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yang
 
Tue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliuTue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliu
 
1000節重要經文
1000節重要經文1000節重要經文
1000節重要經文
 
Tue cheng chang
Tue cheng changTue cheng chang
Tue cheng chang
 
2 25-14
2 25-142 25-14
2 25-14
 
Tue 040114 the call fanzhang
Tue 040114 the call fanzhangTue 040114 the call fanzhang
Tue 040114 the call fanzhang
 
GOD'S ATTRIBUTES
GOD'S ATTRIBUTESGOD'S ATTRIBUTES
GOD'S ATTRIBUTES
 
God's attributes
God's attributesGod's attributes
God's attributes
 
英文常用語排行榜
英文常用語排行榜英文常用語排行榜
英文常用語排行榜
 
Love-An Essence of All Religions
Love-An Essence of All ReligionsLove-An Essence of All Religions
Love-An Essence of All Religions
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similaire à 15 interfaces

Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programmingdeffa5
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdfSudhanshiBakre1
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
11 advance inheritance_concepts
11 advance inheritance_concepts11 advance inheritance_concepts
11 advance inheritance_conceptsArriz San Juan
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.pptVarunP31
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 

Similaire à 15 interfaces (20)

Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
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
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
 
Java 06
Java 06Java 06
Java 06
 
11 advance inheritance_concepts
11 advance inheritance_concepts11 advance inheritance_concepts
11 advance inheritance_concepts
 
Java basics
Java basicsJava basics
Java basics
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
 
Interfaces
InterfacesInterfaces
Interfaces
 
Oop
OopOop
Oop
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 

Plus de dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

Plus de dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

15 interfaces

  • 2. Some classes are better of just defining common behavior You also want to prevent possibility of creating instances of this class Make the class abstract public class Shape{ public abstract void draw() { } } Abstract class
  • 3. A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class itself must be qualified as abstract public abstract class Shape { public abstract void draw(); } Abstract Class
  • 4. You cannot create an object of a class declared abstract If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword. It’s possible to make a class abstract without including any abstract methods You can have methods in an abstract class which have reusable code and they themselves are not declared abstract Abstract Class
  • 5. Create an abstract Java class with one abstract method and another non abstract method Try to create an instance of the abstract class Excerise
  • 6. The interface keyword produces a completely abstract class, one that provides no implementation at all. It allows the creator to determine method names, argument lists, and return types, but no method bodies. An interface provides only a form, but no implementation. An interface says, "All classes that implement this particular interface will look like this." Any code that uses a particular interface knows what methods might be called for that interface, and that’s all. So the interface is used to establish a "protocol" between classes Interfaces
  • 7. To create an interface, use the interface keyword instead of the class keyword Same access specifiers work as with classes, but mostly public An interface can also contain fields, but these are implicitly static and final. To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword Interfaces
  • 8. public interface Singable { public void sing(); } public class Parrot implements Singable { public void sing() { } public void fly() { } } Interface in Action
  • 9. Design an interface to ensure that Shape class and its children– Rectangle, Square can be drawn Design an interface to ensure that Shape class and its children – Rectangle, Square can be filled with color Now write a Test class and create two references one each of the children and assign them to a Shape variable. Invoke the different methods Now try assigning the Shape references to the interface variables. Try invoking the methods Exercise
  • 10. Just like class it is possible to extend an interface public interface A { public int a(); } public interface B extends a { } Extending an interface
  • 11. Q&A