SlideShare a Scribd company logo
1 of 23
POLYMORPHISM
AND
INTERFACE
HABTAMUW.
Introduction
Poly – means “Many”
Morphs – “Forms”
 Polymorphism: is the ability to treat on objects of any sub-class of a super-
class as if it were an object of the super class.
 There fore, it enables you to write programs that process objects that shares
the same super-class (either directly or indirectly) as if they are all objects of
the super-class.
 Enables you to program in general rather than program in specific.
 In polymorphism objects has the ability of having many forms.
Example.
- A Deer is A Animal
- A Deer is A Vegetarian
- A Deer is A Deer
- The Deer class is considered to be polymorphic. Since it has multiples
inheritance.
◦ Polymorphism is java has two types: these are Method overloading and Method
Overriding.
1. Method Overloading (Compile time polymorphism or static Binding):- a method
overloading means there are several methods present in a class having the same name
but different types/ order/ number of parameters.
- at compile time the JVM knows which method to invoke by checking the method
signatures.
2. Method Overriding (Run time polymorphism or Dynamic Binding):- In
inheritance, a child class provides it’s own implementations to the parents
method, there by overriding the implementation of the present.
- Two methods are said to be overridden only if they have the same signature, that
is the same name and the same parameters.
- The parent’s method is called and Overridden Method and the child’s method is
Overriding Method.
Advantages of polymorphism
1. Method overloading allows method that perform similar or closely related functions to be
accessed through a common name.
2. Method overloading can be implemented on constructors allowing different ways to
initialize objects of a class. This enables you to define multiple constructors for handling
different types of initialization.
3. Method overriding allows a sub class to use all the general definitions that a super-class
provides and adds specialized definitions through overridden methods.
4. Method overriding works together with inheritance to enable code re-use of existing class
without the need for recompilation.
Abstract class
Abstraction: is a process of hiding the implementation details and showing only
functionality to the user.
 There are some ways to achieve abstraction:
1. Abstract class
2. Interface
1. Abstract class: is a class in which declared with the abstract keyword
 It can have abstract and non-abstract methods.
 It needs to be extended and it’s method implemented.
 It can’t be Instantiated.
Abstract Method: is a method which is declared as abstract and doesn’t have
implementation.
Rules of java abstract class
 It can’t be Instantiated they can be sub-classed.
 It needs to be declared with abstract keyword.
 It can have final methods.
 It can have constructors and static methods.
E.g abstract voii moviio(ioubli iiltaii ioubli iiltai)i
Interface
 An interface describes a set of methods that can be called on object, but
doesn’t provide concrete implementations for all methods.
 An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
 It is a collection of abstract methods. A class can implements an interface
there by inhering the abstract methods of the interface.
 Once a class implements an interface, all the objects of that class have an
Is – a relationship with the interface type, and all objects of the class are
guaranteed to provide the functionality described by the interface.
Why we use interfaces in java?
The relationship between classes and interfaces?
 Interface can’t be instantiated.
 Interface doesn’t contain any constructor.
 All the methods of interface are abstract.
 Static and final fields can only appear in interface.
 An interface is not extended by a class; it is implemented by a class.
 An interface is implicitly abstract.
 Each method in an interface is also implicit.
 Methods in an interface are implicitly public.
Difference between interface and class
Interface properties
Declaring an interface
Implementing an interface
◦ A class use implements keyword to implement an interface.
Extending interfaces
 An interface can extend another interface in the same way that a class can
extend another class using extend keyword, results a child interface inherits
methods of the parent interface.
 A java class can only extend one parent class. However an interface can
extend more than one parent interface.
e.g. public iitirfaci iociii iitiiis sportsi iviits {
}
Extending multiple interfaces
Multiple inheritance in Java by interface
─ If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Interface inheritance
─ A class implements an interface, but one interface extends another interface.

More Related Content

Similar to Object Oriented Programming - Polymorphism and Interfaces

Similar to Object Oriented Programming - Polymorphism and Interfaces (20)

Core java questions
Core java questionsCore java questions
Core java questions
 
Java Interview Questions For Freshers
Java Interview Questions For FreshersJava Interview Questions For Freshers
Java Interview Questions For Freshers
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Java Core
Java CoreJava Core
Java Core
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java session2
Java session2Java session2
Java session2
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Unit 4
Unit 4Unit 4
Unit 4
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 

Recently uploaded (20)

Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 

Object Oriented Programming - Polymorphism and Interfaces

  • 2. Introduction Poly – means “Many” Morphs – “Forms”  Polymorphism: is the ability to treat on objects of any sub-class of a super- class as if it were an object of the super class.  There fore, it enables you to write programs that process objects that shares the same super-class (either directly or indirectly) as if they are all objects of the super-class.  Enables you to program in general rather than program in specific.  In polymorphism objects has the ability of having many forms.
  • 3. Example. - A Deer is A Animal - A Deer is A Vegetarian - A Deer is A Deer - The Deer class is considered to be polymorphic. Since it has multiples inheritance.
  • 4. ◦ Polymorphism is java has two types: these are Method overloading and Method Overriding. 1. Method Overloading (Compile time polymorphism or static Binding):- a method overloading means there are several methods present in a class having the same name but different types/ order/ number of parameters. - at compile time the JVM knows which method to invoke by checking the method signatures.
  • 5.
  • 6. 2. Method Overriding (Run time polymorphism or Dynamic Binding):- In inheritance, a child class provides it’s own implementations to the parents method, there by overriding the implementation of the present. - Two methods are said to be overridden only if they have the same signature, that is the same name and the same parameters. - The parent’s method is called and Overridden Method and the child’s method is Overriding Method.
  • 7.
  • 8. Advantages of polymorphism 1. Method overloading allows method that perform similar or closely related functions to be accessed through a common name. 2. Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initialization. 3. Method overriding allows a sub class to use all the general definitions that a super-class provides and adds specialized definitions through overridden methods. 4. Method overriding works together with inheritance to enable code re-use of existing class without the need for recompilation.
  • 9. Abstract class Abstraction: is a process of hiding the implementation details and showing only functionality to the user.  There are some ways to achieve abstraction: 1. Abstract class 2. Interface 1. Abstract class: is a class in which declared with the abstract keyword  It can have abstract and non-abstract methods.  It needs to be extended and it’s method implemented.  It can’t be Instantiated. Abstract Method: is a method which is declared as abstract and doesn’t have implementation.
  • 10. Rules of java abstract class  It can’t be Instantiated they can be sub-classed.  It needs to be declared with abstract keyword.  It can have final methods.  It can have constructors and static methods. E.g abstract voii moviio(ioubli iiltaii ioubli iiltai)i
  • 11.
  • 12.
  • 13. Interface  An interface describes a set of methods that can be called on object, but doesn’t provide concrete implementations for all methods.  An interface in Java is a blueprint of a class. It has static constants and abstract methods.  It is a collection of abstract methods. A class can implements an interface there by inhering the abstract methods of the interface.  Once a class implements an interface, all the objects of that class have an Is – a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface.
  • 14. Why we use interfaces in java?
  • 15. The relationship between classes and interfaces?
  • 16.  Interface can’t be instantiated.  Interface doesn’t contain any constructor.  All the methods of interface are abstract.  Static and final fields can only appear in interface.  An interface is not extended by a class; it is implemented by a class.  An interface is implicitly abstract.  Each method in an interface is also implicit.  Methods in an interface are implicitly public. Difference between interface and class Interface properties
  • 17. Declaring an interface Implementing an interface ◦ A class use implements keyword to implement an interface.
  • 18.
  • 19. Extending interfaces  An interface can extend another interface in the same way that a class can extend another class using extend keyword, results a child interface inherits methods of the parent interface.  A java class can only extend one parent class. However an interface can extend more than one parent interface. e.g. public iitirfaci iociii iitiiis sportsi iviits { } Extending multiple interfaces
  • 20. Multiple inheritance in Java by interface ─ If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
  • 21.
  • 22.
  • 23. Interface inheritance ─ A class implements an interface, but one interface extends another interface.