EzDevInfo.com

interface interview questions

Top interface frequently asked interview questions

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?


Source: (StackOverflow)

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python?


Source: (StackOverflow)

Advertisements

The difference between the Runnable and Callable interfaces in Java

What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?


Source: (StackOverflow)

Interface or an Abstract Class: which one to use?

Please explain when I should use an interface and when I should use abstract class?

How I can change my abstract class in to an interface?


Source: (StackOverflow)

Traits vs. Interfaces

I've been trying to study up on PHP lately, and I find myself getting hung up on traits. I understand the concept of horizontal code reuse and not wanting to necessarily inherit from an abstract class. What I don't understand is what is the crucial difference between using traits versus interfaces?

I've tried searching for a decent blog post or article explaining when to use one or the other, but the examples I've found so far seem so similar as to be identical.

Could anyone out there share their opinion/view on this?


Source: (StackOverflow)

What does it mean to "program to an interface"?

I have seen this mentioned a few times and I am not totally clear on what it means. When and why would you do this?

I know what interfaces do, but the fact I am not clear on this makes me think I am missing out on using them correctly.

Is it just so if you were to do:

IInterface classRef = new ObjectWhatever()

You could use any class that implements IInterface? When would you need to do that? The only thing I can think of is if you have a method and you are unsure of what object will be passed expect for it implementing IInterface. I cannot think how often you would need to do that... (Also, how could you write a method that takes in a object that implements an interface? Is that possible?)

Sorry if I completely missed the point.


Additional questions:

  • does using an Interface hit performance?
  • if so how much?
  • how can you avoid it without having to maintain two bits of code?

Source: (StackOverflow)

Should methods in a Java interface be declared with or without a public access modifier?

Should methods in a Java interface be declared with or without the public access modifier?

Technically it doesn't matter, of course. A class method that implements an interface is always public. But what is a better convention?

Java itself is not consistent in this. See for instance Collection vs. Comparable, or Future vs. ScriptEngine.


Source: (StackOverflow)

When to use an interface instead of an abstract class and vice versa?

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage.

When would one want to use an interface and when would one want to use an abstract class?


Source: (StackOverflow)

How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an "Interface" and an "Abstract class".

Here's my response:

Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behaviour.

Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

Members of a Java interface are public by default. A Java abstract class can have the usual flavours of class members like private, protected, etc.

Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

A Java class can implement multiple interfaces but it can extend only one abstract class.

However, the interviewer was not satisfied, and told me that this description represented "book knowledge".

He asked me for a more practical response, explaining when I would choose an abstract class over an interface, using practical examples.

Where did I go wrong?

Thanks in advance.


Source: (StackOverflow)

How are Java interfaces actually used?

So lets say I have this interface:

public interface IBox
{
   public void setSize(int size);
   public int getSize();
   public int getArea();
  //...and so on
}

And I have a class that implements it:

public class Rectangle implements IBox
{
   private int size;
   //Methods here
}

If I wanted to use the interface IBox, i can't actually create an instance of it, in the way:

public static void main(String args[])
{
    Ibox myBox=new Ibox();
}

right? So I'd actually have to do this:

public static void main(String args[])
{
    Rectangle myBox=new Rectangle();
}

If that's true, then the only purpose of interfaces is to make sure that the class which implements an interface has got the correct methods in it as described by an interface? Or is there any other use of interfaces?


Source: (StackOverflow)

Java Pass Method as Parameter

I am looking for a way to pass a parameter by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative.

I've been told interfaces are the alternative to passing methods as parameters but I don't understand how an interface can act as a method by reference. If I understand correctly an interface is simply an abstract set of methods that are not defined. I don't want to send an interface that needs to be defined every time because several different methods could call the same method with the same parameters.

What I would like to accomplish is something similar to this:

public void setAllComponents(Component[] myComponentArray, Method myMethod) {
    for (Component leaf : myComponentArray) {
        if (leaf instanceof Container) { //recursive call if Container
            Container node = (Container) leaf;
            setAllComponents(node.getComponents(), myMethod);
        } //end if node
        myMethod(leaf);
    } //end looping through components
}

invoked such as:

setAllComponents(this.getComponents(), changeColor());
setAllComponents(this.getComponents(), changeSize());

Thanks in advance, Mac


Source: (StackOverflow)

Multiple Inheritance in C#

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

For instance I'm able to implement the missing multiple inheritance pattern using interfaces and three classes like that:

public interface IFirst { void FirstMethod(); }
public interface ISecond { void SecondMethod(); }

public class First:IFirst 
{ 
    public void FirstMethod() { Console.WriteLine("First"); } 
}

public class Second:ISecond 
{ 
    public void SecondMethod() { Console.WriteLine("Second"); } 
}

public class FirstAndSecond: IFirst, ISecond
{
    First first = new First();
    Second second = new Second();
    public void FirstMethod() { first.FirstMethod(); }
    public void SecondMethod() { second.SecondMethod(); }
}

Every time I add a method to one of the interfaces I need to change the class FirstAndSecond as well.

Is there a way to inject multiple existing classes into one new class like it is possible in C++?

Maybe there is a solution using some kind of code generation?

Or it may look like this (imaginary c# syntax):

public class FirstAndSecond: IFirst from First, ISecond from Second
{ }

So that there won't be a need to update the class FirstAndSecond when I modify one of the interfaces.


EDIT

Maybe it would be better to consider a practical example:

You have an existing class (e.g. a text based TCP client based on ITextTcpClient) which you do already use at different locations inside your project. Now you feel the need to create a component of your class to be easy accessible for windows forms developers.

As far as I know you currently have two ways to do this:

  1. Write a new class that is inherited from components and implements the interface of the TextTcpClient class using an instance of the class itself as shown with FirstAndSecond.

  2. Write a new class that inherits from TextTcpClient and somehow implements IComponent (haven't actually tried this yet).

In both cases you need to do work per method and not per class. Since you know that we will need all the methods of TextTcpClient and Component it would be the easiest solution to just combine those two into one class.

To avoid conflicts this may be done by code generation where the result could be altered afterwards but typing this by hand is a pure pain in the ass.


Source: (StackOverflow)

What is the point of interfaces in PHP?

Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods.

Abstract classes allow you to do the same thing, along with adding code to the method.

Now if you can achieve the same goal with abstract classes, why do we even need the concept of interfaces?

I've been told that it has to do with OO theory from C++ to Java, which is what PHP's OO stuff is based on. Is the concept useful in Java but not in PHP? Is it just a way to keep from having placeholders littered in the abstract class? Am I missing something?


Source: (StackOverflow)

Final arguments in interface methods - what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.:

public interface Foo {
    public void foo(int bar, final int baz);
}

public class FooImpl implements Foo {

    @Override
    public void foo(final int bar, int baz) {
        ...
    }
}

In the above example, bar and baz has the opposite final definitions in the class VS the interface.

In the same fashion, no final restrictions are enforced when one class method extends another, either abstract or not.

While final has some practical value inside the class method body, is there any point specifying final for interface method parameters?


Source: (StackOverflow)

Custom fonts and XML layouts (Android)

I'm trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (e.g. one you've placed in assets/font/) in XML files and you can only use the system installed fonts.

I know that, in the Java code, I could change the font of each widget manually using unique IDs. Alternatively, I could iterate over all the widgets in Java to make this change, but this would probably be very slow.

What other options do I have? Is there any better ways to making widgets that have a custom look? I don't particularly want to have to manually change the font for every new widget I add.


Source: (StackOverflow)