Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 325587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:13:50+00:00 2026-05-12T09:13:50+00:00

Just as a counterpoint to this question : what is an interface in Java?

  • 0

Just as a counterpoint to this question: what is an interface in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:13:50+00:00Added an answer on May 12, 2026 at 9:13 am

    An interface is a special form of an abstract class which does not implement any methods. In Java, you create an interface like this:

    interface Interface
    {
        void interfaceMethod();
    }
    

    Since the interface can’t implement any methods, it’s implied that the entire thing, including all the methods, are both public and abstract (abstract in Java terms means “not implemented by this class”). So the interface above is identical to the interface below:

    public interface Interface
    {
        abstract public void interfaceMethod();
    }
    

    To use this interface, you simply need to implement the interface. Many classes can implement an interface, and a class can implement many interfaces:

    interface InterfaceA
    {
         void interfaceMethodA();
    }
    
    interface InterfaceB
    {
        void interfaceMethodB();
    }
    
    public class ImplementingClassA
        implements InterfaceA, InterfaceB
    {
        public void interfaceMethodA()
        {
            System.out.println("interfaceA, interfaceMethodA, implementation A");
        }
    
        public void interfaceMethodB()
        {
            System.out.println("interfaceB, interfaceMethodB, implementation A");
        }
    }
    
    public class ImplementingClassB
        implements InterfaceA, InterfaceB
    {
        public void interfaceMethodA()
        {
             System.out.println("interfaceA, interfaceMethodA, implementation B");
        }
    
        public void interfaceMethodB()
        {
            System.out.println("interfaceB, interfaceMethodB, implementation B");
        }
    }
    

    Now if you wanted you could write a method like this:

    public void testInterfaces()
    {
        ImplementingClassA u = new ImplementingClassA();
        ImplementingClassB v = new ImplementingClassB();
        InterfaceA w = new ImplementingClassA();
        InterfaceA x = new ImplementingClassB();
        InterfaceB y = new ImplementingClassA();
        InterfaceB z = new ImplementingClassB();
    
        u.interfaceMethodA();
        // prints "interfaceA, interfaceMethodA, implementation A"
        u.interfaceMethodB();
        // prints "interfaceB, interfaceMethodB, implementation A"
        v.interfaceMethodA();
        // prints "interfaceA, interfaceMethodA, implementation B"
        v.interfaceMethodB();
        // prints "interfaceB, interfaceMethodB, implementation B"
        w.interfaceMethodA();
        // prints "interfaceA, interfaceMethodA, implementation A"
        x.interfaceMethodA();
        // prints "interfaceA, interfaceMethodA, implementation B"
        y.interfaceMethodB();
        // prints "interfaceB, interfaceMethodB, implementation A"
        z.interfaceMethodB();
        // prints "interfaceB, interfaceMethodB, implementation B"
    }
    

    However, you could never do the following:

    public void testInterfaces()
    {
        InterfaceA y = new ImplementingClassA();
        InterfaceB z = new ImplementingClassB();
    
        y.interfaceMethodB(); // ERROR!
        z.interfaceMethodA(); // ERROR!
    }
    

    The reason you can’t do this is that y is of type interfaceA, and there is no interfaceMethodB() in interfaceA. Likewise, z is of type interfaceB and there is no interfaceMethodA() in interfaceB.

    I mentioned earlier that interfaces are just a special form of an abstract class. To illustrate that point, look at the following code.

    interface Interface
    {
        void abstractMethod();
    }
    
    abstract public class AbstractClass
    {
        abstract public void abstractMethod();
    }
    

    You would inherit from these classes almost exactly the same way:

    public class InheritsFromInterface
        implements Interface
    {
        public void abstractMethod() { System.out.println("abstractMethod()"); }
    }
    
    public class InteritsFromAbstractClass
        extends AbstractClass
    {
        public void abstractMethod() { System.out.println("abstractMethod()"); }
    }
    

    In fact, you could even change the interface and the abstract class like this:

    interface Interface
    {
        void abstractMethod();
    }
    
    abstract public class AbstractClass
        implements Interface
    {
        abstract public void abstractMethod();
    }
    
    public class InheritsFromInterfaceAndAbstractClass
        extends AbstractClass implements Interface
    {
        public void abstractMethod() { System.out.println("abstractMethod()"); }
    }
    

    However, there are two differences between interfaces and abstract classes.

    The first difference is that interfaces cannot implement methods.

    interface Interface
    {
        public void implementedMethod()
        {
            System.out.println("implementedMethod()");
        }
    }
    

    The interface above generates a compiler error because it has an implementation for implementedMethod(). If you wanted to implement the method but not be able to instantiate the class, you would have to do it like this:

    abstract public class AbstractClass
    {
        public void implementedMethod()
        {
            System.out.println("implementedMethod()");
        }
    }
    

    That’s not much of an abstract class because none of its members are abstract, but it is legal Java.

    The other difference between interfaces and abstract classes is that a class can inherit from multiple interfaces, but can only inherit from one abstract class.

    abstract public class AbstractClassA { }
    abstract public class AbstractClassB { }
    public class InheritsFromTwoAbstractClasses
        extends AbstractClassA, AbstractClassB
    { }
    

    The code above generates a compiler error, not because the classes are all empty, but because InheritsFromTwoAbstractClasses is trying to inherit from two abstract classes, which is illegal. The following is perfectly legal.

    interface InterfaceA { }
    interface InterfaceB { }
    public class InheritsFromTwoInterfaces
        implements InterfaceA, InterfaceB
    { }    
    

    The first difference between interfaces and abstract classes is the reason for the second difference. Take a look at the following code.

    interface InterfaceA
    {
        void method();
    }
    
    interface InterfaceB
    {
        void method();
    }
    
    public class InheritsFromTwoInterfaces
        implements InterfaceA, InterfaceB
    {
        void method() { System.out.println("method()"); }
    }
    

    There’s no problem with the code above because InterfaceA and InterfaceB don’t have anything to hide. It’s easy to tell that a call to method will print “method()”.

    Now look at the following code:

    abstract public class AbstractClassA
    {
        void method() { System.out.println("Hello"); }
    }
    
    abstract public class AbstractClassB
    {
        void method() { System.out.println("Goodbye"); }
    }
    
    public class InheritsFromTwoAbstractClasses
        extends AbstractClassA, AbstractClassB
    { }
    

    This is exactly the same as our other example, except that because we’re allowed to implement methods in abstract classes, we did, and because we don’t have to implement already-implemented methods in an inheriting class, we didn’t. But you may have noticed, there’s a problem. What happens when we call new InheritsFromTwoAbstractClasses().method()? Does it print “Hello” or “Goodbye”? You probably don’t know, and neither does the Java compiler. Another language, C++ allowed this kind of inheritance and they resolved these issues in ways that were often very complicated. To avoid this kind of trouble, Java decided to make this “multiple inheritance” illegal.

    The downside to Java’s solution that the following can’t be done:

    abstract public class AbstractClassA
    {
        void hi() { System.out.println("Hello"); }
    }
    
    abstract public class AbstractClassB
    {
        void bye() { System.out.println("Goodbye"); }
    }
    
    public class InheritsFromTwoAbstractClasses
        extends AbstractClassA, AbstractClassB
    { }
    

    AbstractClassA and AbstractClassB are “mixins” or classes that aren’t intended to be instantiated but add functionality to the classes that they are “mixed into” through inheritance. There’s obviously no problem figuring out what happens if you call new InheritsFromTwoAbstractClasses().hi() or new InheritsFromTwoAbstractClasses().bye(), but you can’t do that because Java doesn’t allow it.

    (I know this is a long post, so if there are any mistakes in it please let me know and I will correct them.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is what the Profiling API is for. See ICorProfilerCallback2::GarbageCollectionStarted… May 12, 2026 at 4:14 pm
  • Editorial Team
    Editorial Team added an answer For a datagrid you can override the DrawRowBackground method of… May 12, 2026 at 4:14 pm
  • Editorial Team
    Editorial Team added an answer Rally fits the bill, is quite good, and the community… May 12, 2026 at 4:14 pm

Related Questions

I imagine that this is a simple question, but I'm getting some strange results
Greetings! I'm attempting to use MKMapView without any Apple code samples, though there are
This question was bugging me for quite a while (as evidenced by my previous
Just as a personal experiment, in order to try to learn better about programming

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.