What is an interface in Java?

An interface in Java is a blueprint of a class that includes static constants and abstract methods. Interfaces are used to provide a common set of methods that can be accessed and used by any class, regardless of its specific implementation.

Example:

public interface Animal {
public void eat();
public void move();
}

public class Dog implements Animal {
public void eat() {
System.out.println(“The dog is eating.”);
}
public void move() {
System.out.println(“The dog is running.”);
}
}

What is the difference between an interface and an abstract class in Java?

An interface is a collection of abstract methods, meaning that they are not implemented in the interface itself. Interfaces can also contain constants, default methods, static methods, and nested types. An example of an interface in Java is:

public interface Animal {
public void eat();
public void sleep();
}

An abstract class is a class that contains both abstract and non-abstract methods. Abstract classes can also contain constants, default methods, static methods, and nested types. An example of an abstract class in Java is:

public abstract class Animal {
public abstract void eat();
public void sleep(){
System.out.println(“Sleeping…”);
}
}

What is the difference between an interface and an abstract class?

An interface is a collection of abstract methods that must be implemented by any class that implements the interface. An abstract class is a class that contains one or more abstract methods that must be overridden by any class that extends the abstract class.

Example:

Interface:

public interface Shape {
public void draw();
public double getArea();
}

Abstract Class:

public abstract class Shape {
public abstract void draw();
public abstract double getArea();
}

What is the difference between a protocol and an interface in Swift?

A protocol is a set of rules that defines how two objects interact with each other. It defines the methods, properties, and other requirements that must be implemented by any class that conforms to it. For example, the UITableViewDataSource protocol defines the methods that a class must implement to be used as a data source for a UITableView.

An interface, on the other hand, is a collection of related functions and variables that are declared, but not defined. It allows a class to specify its external behavior without providing an implementation for the behavior. For example, the UITableViewDelegate protocol declares the methods that a class must implement to be used as a delegate for a UITableView.

What is the difference between an interface and an abstract class?

An interface is a collection of abstract methods and constants that form a common set of base rules for a class to follow. An interface does not contain any implementation code, and it cannot be instantiated. An example of an interface might be a Comparable interface which defines the compareTo() method.

An abstract class is a class that contains both abstract methods (methods without an implementation) and concrete methods (methods with an implementation). An abstract class can be instantiated, and it is often used as a base class from which other classes can inherit. An example of an abstract class might be an Animal class which defines the abstract method makeSound() and the concrete method eat().

What is the purpose of the @interface and @implementation directives?

The @interface and @implementation directives are used to define a class in Objective-C.

@interface defines the class interface, which includes the class name, the superclass it inherits from, and any methods, properties, and instance variables the class has.

@implementation defines the class implementation, which includes the actual code for the methods, properties, and instance variables declared in the interface.

Example:

@interface MyClass : NSObject

@property (nonatomic, strong) NSString *name;

– (void)sayHello;

@end

@implementation MyClass

@synthesize name;

– (void)sayHello {
NSLog(@”Hello %@”, self.name);
}

@end