Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • Re: General CS concepts

    Interfaces

    Interfaces are also known as purely abstract classes: they contain only abstract methods. An abstract method is one that has not been implemented, but defines how that method is called and what to expect back as output. It is the job of inheriting classes to implement the abstract methods.

    A quick note about interfaces: while Java doesn't allow you to have one class that inherits from multiple sources, that one class can implement multiple interfaces. There are reasons for this, but I won't discuss it here (personally, I wish Java did allow multiple inheritance and got rid of the interfaces, but that's just me).

    Interfaces may have fields and methods, but they can ONLY have the following modifiers on them: public, static (fields only), abstract (methods only), and/or final (fields only). The reason why you can't have private fields/methods is because they can never be used, because your interface is basically a dummy class. The same holds for protected: they can only be used by inheriting classes, but in java only interfaces can inherit from another interface, thus the protected fields/methods can never be used.

    Special note about declaration modifiers (Thanks to Json):

    If you leave off the modifiers public,static, and/or final, Java will implicitly add them. All fields must be public, static, and final, and all methods must be public. All methods must be public and abstract.
    Example: say you typed this in:
    public interface A
    {
         int a = 5;
         void doIt();
    }

    Java will implicitly change it to this:
    public interface A
    {
         public static final int a = 5;
         public abstract void doIt();
    }

    Classes must implement interfaces, not inherit from them. This means exactly that: any class that says they will implement an interface must include all the methods within that interface. It doesn't matter if the implemented method does nothing, though.

    *Special note (thanks to Json): Abstract classes don't need to implement all the methods in an interface. However, any concrete (non-abstract) class that either inherits from an abstract class, or implements that interface MUST implement all the methods.

    Ex:
    public interface Doable
    {
         public final int myInt = 10;
         public boolean canDoIt1();
         public int doIt2(int num);
    }

    Notice how in the above interface you don't need to put the abstract modifier (but you can if you want) in the method signiture. This is because by definition, they must be abstract to be in an interface. Now, let's create a class that implements this interface:

    public class Poco implements Doable
    {
         // This method won't do anything
         public boolean canDoIt1()
         {
              return false;
         }
     
         public int doIt2(int num)
         {
              return 2*num;
         }
    }

    Here, class Poco must have declared inside all the methods within the interface it implements. However, we can implement the methods any way we want. Good programming practice dictates that we stick with the documentation in the interface of how the method SHOULD behave, though. That's the point of interfaces. An analogy is a Drivable interface. If we had a method called turnLeft that turned our vehicle left, we wouldn't want an implementing class to decide that the turnLeft method actually accelerates the car, though the compiler can't and won't prevents this from happening.

    Here are some questions concerning interfaces:

    1. What is an interface?

    2. How do interfaces differ from purely abstract classes in Java?

    3. What can and can't go in an interface?

    Answers are coming later

    Happy coding
    This article was originally published in forum thread: General CS concepts started by helloworld922 View original post