Java Interface by Ayush Assisgment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Interface ASSIGNMENT

Ques 1.What is interface in java?


ANS - The interface in Java is a mechanism to achieve abstraction.
There can be only abstract methods in the Java interface, not the
method body. It is used to achieve abstraction and multiple
inheritances in Java using Interface. In other words, you can say that
interfaces can have abstract methods and variables. It cannot have a
method body.
interface {
// declare constant fields
// declare methods that abstract
// by default.
}

Ques 2.Which modifiers are allowed for methods


in an Interface ? Explain with an example.
Ans –
1.public: All methods in an interface are implicitly public, meaning they
can be accessed from anywhere. This is because interfaces are meant to
define contracts for classes to implement, and these methods need to
be accessible to the implementing classes.
2.default: Starting from Java 8, interfaces can have default methods,
which provide default implementations for methods. Default methods
are used to introduce new functionality to interfaces without breaking
existing code. They are declared using the default keyword and can
have method bodies.
interface Shape {
// Public method declaration
public void draw();

// Default method with a method body


default void display() {
System.out.println("Displaying shape...");
}
}

// Implementation of the Shape interface


class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing circle...");
}
}

public class Main {


public static void main(String[] args) {
Circle circle = new Circle();
circle.draw(); // Calling draw method
circle.display(); // Calling default method
}
}
Ques 3.What is the use of interface in java ?Or,
why , do we use an Interface in java ?
Ans - In Java, interfaces are used to define a contract for classes to
adhere to. They provide a way to specify a set of methods that a class
implementing the interface must implement. Interfaces are widely used
in Java for the following reasons:

1. Abstraction: Interfaces allow you to define a set of methods


without providing any implementation details. This allows you to
define what needs to be done without specifying how it should be
done. This level of abstraction is essential for designing modular
and maintainable code.
2. Multiple Inheritance: Java does not support multiple inheritance
for classes, meaning a class cannot extend more than one class.
However, a class can implement multiple interfaces. This allows
Java to achieve a form of multiple inheritance through interfaces,
enabling a class to inherit behaviors from multiple sources.
3. Polymorphism: Interfaces enable polymorphic behavior, where
objects of different classes can be treated as objects of a common
interface type. This is particularly useful for writing code that can
work with objects of various classes as long as they implement a
common interface. This promotes code reusability and flexibility.
4. Decoupling: Interfaces help in decoupling the implementation
from the usage. By programming to an interface rather than a
concrete implementation, you can write code that is more flexible
and easier to maintain. This allows for easier swapping of
implementations without affecting the client code.
5. Design Patterns: Many design patterns in Java, such as the
Factory Pattern, Adapter Pattern, and Strategy Pattern, rely
heavily on interfaces to define contracts and provide flexibility in
implementations. Interfaces facilitate the implementation of these
design patterns by enforcing a clear separation of concerns.
6. API Contracts: Interfaces are often used to define contracts in
APIs. By defining interfaces, API designers can specify what
methods classes must implement to be compatible with the API.
This allows for better documentation and ensures consistency
and interoperability among different implementations.

Ques 4.What is the difference between abstract


class and interface in java?
Abstract class Interface

1) Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and static
methods also.

2) Abstract class doesn't support multiple Interface supports multiple inheritance.


inheritance.

3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.

4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.

5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.

6) An abstract class can extend another An interface can extend another Java interface
Java class and implement multiple Java only.
interfaces.

7) An abstract class can be extended using An interface can be implemented using


keyword "extends". keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.

9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

You might also like