SlideShare ist ein Scribd-Unternehmen logo
1 von 4
INTERFACE VS ABSTARCT CLASS
        Ever thought of their differences in real time? This is the frequent important question asked in
the interviews. Many people give a canned response like “You can define default functionality in an
abstract class and you can just define functions in an interface”. The curve ball is thrown when you ask
“Why would you use one over the other?” That will earn you the ‘deer in headlights’ look. Some reply “I
never had to use that so I don’t know”. So what are the great differences these two entities have in
Software Development?

At the top level, there are a few basic differences. Abstract classes allow for default function definition.
This means that whatever class extends the abstract class will have access to this. If we have a base class
where all the classes will perform the same function, then we can define that in our Abstract class. An
interface is a list of functions or properties that if a class implements it, it will have to have those
functions defined within it. It is a situation of “Is-A” vs. “Can-Do-this”. Objects that extends an Abstract
class “Is-A” base class. Objects that implement “Can-Do-This”. Now if I asked this question and got the
answer, yes, that would be the correct answer. However, I want to know why one would want to use an
interface over an abstract class, and vice versa.
In this post I explain where exactly the interface is used and so do the abstract class.

Interface and its use:
    •      It is Purely Abstract
    •      It’s having Fixed Functionality
    •      If an Interface changed, all the underlined classes will change.

Example:

  Inject        Business Method to an Interface A (New Story or Feature)



                           Where to add it?



Belongs to Interface A?                      Not belongs to Existing Interfaces?

Add to Interface A                           Add a new Interface B

All underlying class changes                  May be used in new classes or some (not all) of the
                                                 Underlying classes of Interface A

Called Extensibility of Interface A           Called Multiple Inheritance.



    •      This scenario is not applicable when Interfaces (fixed) are sold across Business.
               o Example: Service Contracts
• This is used when no need of Massive hierarchical framework.
Example 1:
             Inherits         implements
Interface A           Class B <--------------- Class B


Example 2:

Let’s have an interface for a Director and another interface for an Actor.
        Public interface Actor {
           Performance say (Line l);
        }

        Public interface Director {
          Movie direct (boolean goodMovie);
        }

    •   In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract
        classes, we can implement both Actor and Director.
    • We could even define an ActorDirector interface that extends both like this:
      Public interface ActorDirector extends Actor, Director {
                           ...
      }
    • We could achieve the same thing using abstract classes.
    • By using Abstract class would require up to 2^n (where n is the number of attributes) possible
        combinations in order to support all possibilities.
        Ex: ActorDirector, DirectorActor…..

Why to prefer an Abstract Class?

        Abstract classes allow you to provide default functionality for the subclasses. Common
knowledge at this point. Why is this extremely important though? If you plan on updating this base class
throughout the life of your program, it is best to allow that base class to be an abstract class. Why?
Because you can make a change to it and all of the inheriting classes will now have this new
functionality. If the base class will be changing often and an interface was used instead of an abstract
class, we are going to run into problems. Once an interface is changed, any class that implements that
will be broken. Now if it’s just you working on the project, that’s no big deal. However, once your
interface is published to the client, that interface needs to be locked down. At that point, you will be
breaking the client’s code.
     • Its contains Default Functionality & abstract methods


                Concrete Methods


                Used in Versioning by overriding the default functionality in new version (class that
                extends previous abstract class)
•   Abstraction used with polymorphism is called Versioning.
        •   If we have common functionality used in a hierarchy of classes then put them in Abstract class.


    Whenever we need to override the existing functionality we should cut a branch of underlying classes
    and inherit from parent abstract class (which contains common functionality).
    Example: Some common functionality is used across the hierarchy B-C-D-E-F. If you want to change the
    functionality how the D object works then you need to cut the tree and attach E-F to A.

                       Common                                                      Common
                       functionalit       Abstract Class                           functionalit
                       y                     AA                                    y
                                                A
                                              A

                                                B


                                                C


A                                                                                    Used
                                                D
    Overridden
    With new functionality

    Cut Tree
E                                               E



F                                               F




            Frameworks are a good place to show when and where to use both an abstract class and an
    interface. Another general rule is if you are creating something that provides common functionality to
    unrelated classes, use an interface. If you are creating something for objects that are closely related in a
    hierarchy, use an abstract class. An example of this would be something like a business rules engine.
    This engine would take in multiple BusinessRules as classes perhaps? Each one of these classes will have
    an analyze function on it.

            public interface BusinessRule{
              Boolean analyze(Object o);
            }
This can be used ANYWHERE. It can be used to verify the state of your application. Verify data is
correct. Verify that the user is logged in. Each one of these classes just needs to implement the analyze
function, which will be different for each rule.
         Where as if we were creating a generic List object, the use of abstract classes would be better.
Every single List object is going to display the data in a list in some form or another. The base
functionality would be to have it go through its dataprovider and build that list. If we want to change
that List object, we just extend it, override our build list function, change what we want and call
super.buildList();
         Almost everyone knows that interfaces means you are just defining a list of functions and that
abstract classes has the option of providing default functionality. The snags come when you drop the
‘why would I use one over the other?’ Abstract classes and interfaces are some of the most important
fundamentals of object oriented programming. Just knowing the differences between the two is not
enough. When you can look at a situation and make a strong recommendation, you will know you have
a much stronger knowledge of object oriented programming. Also it helps during interviews.

Weitere ähnliche Inhalte

Was ist angesagt?

Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 
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 Singhdheeraj_cse
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
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 InterfacesAhmed Nobi
 
Adapter 2pp
Adapter 2ppAdapter 2pp
Adapter 2pprajjani
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Design patterns difference between interview questions
Design patterns   difference between interview questionsDesign patterns   difference between interview questions
Design patterns difference between interview questionsUmar Ali
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
Chapter 4: Enhanced Entity-Relationship and Object Modeling
Chapter 4:  Enhanced Entity-Relationship and Object ModelingChapter 4:  Enhanced Entity-Relationship and Object Modeling
Chapter 4: Enhanced Entity-Relationship and Object ModelingRaj vardhan
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Interface java
Interface java Interface java
Interface java atiafyrose
 
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 | EdurekaEdureka!
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
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
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 

Was ist angesagt? (20)

Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
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
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
javainterface
javainterfacejavainterface
javainterface
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
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
 
Adapter 2pp
Adapter 2ppAdapter 2pp
Adapter 2pp
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Design patterns difference between interview questions
Design patterns   difference between interview questionsDesign patterns   difference between interview questions
Design patterns difference between interview questions
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
 
Chapter 4: Enhanced Entity-Relationship and Object Modeling
Chapter 4:  Enhanced Entity-Relationship and Object ModelingChapter 4:  Enhanced Entity-Relationship and Object Modeling
Chapter 4: Enhanced Entity-Relationship and Object Modeling
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Interface java
Interface java Interface java
Interface java
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
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
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 

Andere mochten auch

Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming LanguageJunji Zhi
 
Adjacency And Incidence Matrix
Adjacency And Incidence MatrixAdjacency And Incidence Matrix
Adjacency And Incidence MatrixAbir Junayed
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
4. python functions
4. python   functions4. python   functions
4. python functionsin4400
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagramsbarney92
 

Andere mochten auch (9)

Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Adjacency And Incidence Matrix
Adjacency And Incidence MatrixAdjacency And Incidence Matrix
Adjacency And Incidence Matrix
 
Interface
InterfaceInterface
Interface
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagrams
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Ähnlich wie Interface Vs Abstact

Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldSaurabh Moody
 
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
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1LK394
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface pptmanojsharma469262
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdfSudhanshiBakre1
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design PatternsDang Tuan
 

Ähnlich wie Interface Vs Abstact (20)

Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
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)
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface ppt
 
Concepts of oops
Concepts of oopsConcepts of oops
Concepts of oops
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Unit 4
Unit 4Unit 4
Unit 4
 
Oop
OopOop
Oop
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
Creational Design Patterns
Creational Design PatternsCreational Design Patterns
Creational Design Patterns
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
 
Java mcq
Java mcqJava mcq
Java mcq
 

Mehr von Anand Kumar Rajana (13)

Anand's Leadership Assessment
Anand's Leadership AssessmentAnand's Leadership Assessment
Anand's Leadership Assessment
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Rhino Mocks
Rhino MocksRhino Mocks
Rhino Mocks
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
The Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.NetThe Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.Net
 
What Do You Mean By NUnit
What Do You Mean By NUnitWhat Do You Mean By NUnit
What Do You Mean By NUnit
 
Wcf
WcfWcf
Wcf
 
Sql Server 2012 Installation..
Sql Server 2012 Installation..Sql Server 2012 Installation..
Sql Server 2012 Installation..
 
Json
JsonJson
Json
 
Jquery Ajax
Jquery AjaxJquery Ajax
Jquery Ajax
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
J Query Introduction And JQuery Selectors
J Query Introduction And JQuery SelectorsJ Query Introduction And JQuery Selectors
J Query Introduction And JQuery Selectors
 

Kürzlich hochgeladen

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Kürzlich hochgeladen (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Interface Vs Abstact

  • 1. INTERFACE VS ABSTARCT CLASS Ever thought of their differences in real time? This is the frequent important question asked in the interviews. Many people give a canned response like “You can define default functionality in an abstract class and you can just define functions in an interface”. The curve ball is thrown when you ask “Why would you use one over the other?” That will earn you the ‘deer in headlights’ look. Some reply “I never had to use that so I don’t know”. So what are the great differences these two entities have in Software Development? At the top level, there are a few basic differences. Abstract classes allow for default function definition. This means that whatever class extends the abstract class will have access to this. If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. An interface is a list of functions or properties that if a class implements it, it will have to have those functions defined within it. It is a situation of “Is-A” vs. “Can-Do-this”. Objects that extends an Abstract class “Is-A” base class. Objects that implement “Can-Do-This”. Now if I asked this question and got the answer, yes, that would be the correct answer. However, I want to know why one would want to use an interface over an abstract class, and vice versa. In this post I explain where exactly the interface is used and so do the abstract class. Interface and its use: • It is Purely Abstract • It’s having Fixed Functionality • If an Interface changed, all the underlined classes will change. Example: Inject Business Method to an Interface A (New Story or Feature) Where to add it? Belongs to Interface A? Not belongs to Existing Interfaces? Add to Interface A Add a new Interface B All underlying class changes May be used in new classes or some (not all) of the Underlying classes of Interface A Called Extensibility of Interface A Called Multiple Inheritance. • This scenario is not applicable when Interfaces (fixed) are sold across Business. o Example: Service Contracts
  • 2. • This is used when no need of Massive hierarchical framework. Example 1: Inherits implements Interface A Class B <--------------- Class B Example 2: Let’s have an interface for a Director and another interface for an Actor. Public interface Actor { Performance say (Line l); } Public interface Director { Movie direct (boolean goodMovie); } • In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes, we can implement both Actor and Director. • We could even define an ActorDirector interface that extends both like this: Public interface ActorDirector extends Actor, Director { ... } • We could achieve the same thing using abstract classes. • By using Abstract class would require up to 2^n (where n is the number of attributes) possible combinations in order to support all possibilities. Ex: ActorDirector, DirectorActor….. Why to prefer an Abstract Class? Abstract classes allow you to provide default functionality for the subclasses. Common knowledge at this point. Why is this extremely important though? If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Why? Because you can make a change to it and all of the inheriting classes will now have this new functionality. If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken. Now if it’s just you working on the project, that’s no big deal. However, once your interface is published to the client, that interface needs to be locked down. At that point, you will be breaking the client’s code. • Its contains Default Functionality & abstract methods Concrete Methods Used in Versioning by overriding the default functionality in new version (class that extends previous abstract class)
  • 3. Abstraction used with polymorphism is called Versioning. • If we have common functionality used in a hierarchy of classes then put them in Abstract class. Whenever we need to override the existing functionality we should cut a branch of underlying classes and inherit from parent abstract class (which contains common functionality). Example: Some common functionality is used across the hierarchy B-C-D-E-F. If you want to change the functionality how the D object works then you need to cut the tree and attach E-F to A. Common Common functionalit Abstract Class functionalit y AA y A A B C A Used D Overridden With new functionality Cut Tree E E F F Frameworks are a good place to show when and where to use both an abstract class and an interface. Another general rule is if you are creating something that provides common functionality to unrelated classes, use an interface. If you are creating something for objects that are closely related in a hierarchy, use an abstract class. An example of this would be something like a business rules engine. This engine would take in multiple BusinessRules as classes perhaps? Each one of these classes will have an analyze function on it. public interface BusinessRule{ Boolean analyze(Object o); }
  • 4. This can be used ANYWHERE. It can be used to verify the state of your application. Verify data is correct. Verify that the user is logged in. Each one of these classes just needs to implement the analyze function, which will be different for each rule. Where as if we were creating a generic List object, the use of abstract classes would be better. Every single List object is going to display the data in a list in some form or another. The base functionality would be to have it go through its dataprovider and build that list. If we want to change that List object, we just extend it, override our build list function, change what we want and call super.buildList(); Almost everyone knows that interfaces means you are just defining a list of functions and that abstract classes has the option of providing default functionality. The snags come when you drop the ‘why would I use one over the other?’ Abstract classes and interfaces are some of the most important fundamentals of object oriented programming. Just knowing the differences between the two is not enough. When you can look at a situation and make a strong recommendation, you will know you have a much stronger knowledge of object oriented programming. Also it helps during interviews.