Results 1 to 7 of 7

Thread: Do I need to define all the methods when an Interface is implemented ?

  1. #1
    Guest

    Do I need to define all the methods when an Interface is implemented ?

    I am a free lancer, just started to develop application based on the Java language and the .NET platform , although I am not a professional in it , I am in the learning phase , before this I used to develop web sites using the PHP language. I was designing a GUI in one of the project , in which I had to add certain number of controls and buttons and in had to associate those controls with a few events . I know that I need to implement certain interfaces for that . So I have done that . Apart from the implementing the interfaces I am also including a abstract classes in my program . I cannot actually differentiate that do need to define all the methods in the interface . I will recommend programmers and developers to comment on this .

  2. #2
    Join Date
    May 2009
    Posts
    513

    Re: Do I need to define all the methods when an Interface is implemented ?

    An abstract class may include complete or unfinished methods. Interfaces can enclose only the declaration of a method but no body. Thus an abstract class .Is allowed to implement any method but an interface is not permitted to do so. implement methods but an interface can not implement methods.An abstract class can enclose fields, constructors, or destructors and implement it’s behaviors . An interface can not enclose fields, constructors, or destructors and it contains only the property's declaration but not regarding how the properties is being implemented . If not implemented accurately the Abstract class may throw exception.

  3. #3
    Join Date
    May 2009
    Posts
    527

    Re: Do I need to define all the methods when an Interface is implemented ?

    Java actually combines the two ideas together a bit. Java interfaces are good and clean while you "implement" an interface, you are instructing that your class remains to the "contract" of the interface that you mention. Java class inheritance is not so precise - when you derive an class from an existing class in the Java programming language in Java you are getting both the code inheritance but you are also implying that that your child class belongs to the "contract" of the interface of the base class.

  4. #4
    Join Date
    May 2009
    Posts
    640

    Re: Do I need to define all the methods when an Interface is implemented ?

    Abstract classes in Java are just similar to standard Java classes but with the additional restraint that you cannot create an object of them straight away. In terms of that additional constraint, they are essentially classes which do not in fact implement all of the code mentioned by their "contract".So, it's usually considered good Object oriented practice to mention the "contract" which you desire to stick to through Java interfaces. Then make use of standard Java class inheritance mainly for code reuse point. Make use of abstract Java classes when you wish to render some typical base code but wish/require to oblige the user's of your class to finish the implementation .

  5. #5
    Join Date
    May 2009
    Posts
    546

    Re: Do I need to define all the methods when an Interface is implemented ?

    To declare a class that uses an interface, you take in an implements clause in the declaration of a class . Your class is permitted to implement multiple interfaces, so the implements keyword is followed by a comma-separated catalog of the interfaces implemented by the class.
    Take an example of interface that describes how to evaluate the size of objects.

    Code:
    public interface Relatable {
    
           
    
       // this (object invoking isLargerThan) and
    
       // other should be object of the same class
    
       //   public int isLargerThan(Relatable other);
    
    }
    If you want to be capable to contrast the size of related objects, no issue what they are, the class that instantiates them must implement Relatable. Once you are aware that a class implements Relatable, then you are quite familiar that you can contrast the size of the objects created from that class.

  6. #6
    Join Date
    May 2009
    Posts
    542

    Re: Do I need to define all the methods when an Interface is implemented ?

    For instance, take the Collection interface introduced on the preceding page. Now, presume that you were writing a class that implemented a FIFO (first in, first out) queue. Since a FIFO queue object include other instances it makes sensible for the class to put into practice the Collectioninterface. The FIFOQueue class would declare that it implements the Collection interface like this:

    class FIFOQueue implements Collection {

    Statement. . .

    void add(Object ob) {

    . . .

    }

    void delete(Object ob) {

    Statements. . .

    }

    Object find(Object ob) {

    Statement . . .

    }

    int curCnt() {

    Statement . . .

    }

    }

  7. #7
    Join Date
    Apr 2009
    Posts
    491

    Re: Do I need to define all the methods when an Interface is implemented ?

    By declaring that it implements the Collection interface, the FIFOQueue class ascertains that it renders implementations for the mentioned methods and statements on . An interface just contains the declaration part , it does not contain any body of the method. When any class implements an interface the body of the methods presented that are defined in that particular class . So I know that what you are asking actually , you want to ask that if you are implementing any particular interface , is it mandate to define all the methods present in that interface ? Well it is not mandatory at all that you define all the methods. This situation occurs when you are using an Abstract Class . So it is always preferred and recommended to use the interface instead of an abstract class . I think this post was a bit of assistance to you but in case if you find the language difficult then please revert back I will try to make my language more simpler.

Similar Threads

  1. Replies: 9
    Last Post: 16-03-2012, 10:33 AM
  2. Is TRIM firmware is implemented in 128 GB Samsung SSD?
    By AliaShaikh in forum Hardware Peripherals
    Replies: 3
    Last Post: 11-02-2012, 07:56 AM
  3. Replies: 7
    Last Post: 22-10-2011, 10:35 PM
  4. Windows Movie Maker Error: "Not implemented”
    By Operatorr in forum Windows Software
    Replies: 3
    Last Post: 15-12-2010, 04:16 PM
  5. Replies: 3
    Last Post: 05-09-2009, 08:23 AM

Posting Permissions

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