Results 1 to 5 of 5

Thread: About interface

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default About interface

    I have understood that interfaces actually does no impact to algorithms or any serious impact to code all.
    It is just a solution for cooperative programmers to bring all methods into one place.
    Iam right?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: About interface

    Interfaces separate the 'what' from the 'how'. The interface defines a series of method signatures that are essentially a contract that all implementations of that interface must define. The interface doesn't specify any of the 'how', only the 'what'. So, as far as an 'algorithm' is concerned, I believe you may be referring to the actual implementation of a method.

  3. #3
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: About interface

    It can have serious impact. Just like when you're using buttons on TV to change channel. You don't know how it's done and you don't care, all you want to know is that when you press a button the channel changes.
    Same way, when you implement a MouseListener interface, you don't care how it is getting data, you just care to get a MouseEvent evt, that you can use for your purpose.

  4. #4
    rsanju is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default Re: About interface

    Where should we use interface and where should we use abstract class? What is the use of these? Can anyone give me a realtime example

  5. #5
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: About interface

    When you exploit inheritance, you will wind up creating a general parent class and a bunch of children. Since your parent class in a generalization of children, you can end up with just an abstract concept which you don't want instantiated. For example you make a parent class Animals, and children Cat, Dog, Mouse, Bird, Horse. Here you have a concrete animals and you'd want them instantiated sometimes, but there is no precisely defined entity called Animal in real nature. You don't want that one ever instantiated, so you mark it as abstract. Abstract class makes sense only if you extend it. This way you forbid anyone to instantiate undefined Animal, but keep the benefits of polymorphism. Using poly you can a year later add another animal, or a car model (if this was a racing game) and you could still keep the same code.

    About interface-abstract.jpg

    Now, some programming languages have multiple inheritance, but Java doesn't. It means you can extend only one class. That's when you would use an interface. They solve that problem. You can implement multiple interfaces.

    Logic wise, when you have related classes you extend them, but if you have unrelated classes and want to implement the same behavior, you use interface. This way you can add a Toy behavior for a Mouse and Impreza.


    About interface-interface.jpg
    Last edited by cronnin; 02-02-2017 at 05:25 PM.

Similar Threads

  1. Replies: 11
    Last Post: 11-05-2015, 07:39 PM
  2. Replies: 1
    Last Post: 07-08-2012, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •