0% found this document useful (0 votes)
5 views3 pages

Abstract Class vs Interface Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Abstract Class vs Interface Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Diffrence Between Interfcae And Abstract Class:

Interface Abstract Class.

1)All method in Interfcae are 1) Both abstract and non abstract methods
are available .
abstract methods.(till 1.7 version)

2) It supports multiple Inheritance. 2) It doesnt support multiple inheritance.

3) By default interface methods are 3)there are no any restriction on abstract


class method modifier.
public + abstract.

4)By default interface variables are 4) No restriction .we can take another
variables also.
public + static + final.

5) By Using interface keyword we can 5) By using abstract keyword we can create


abstract class.
create interface.

6) Interface doesnt contain constructor. 6) Abstract class contains constructor.

7) Interface can achive 100 % abstraction. 7) In abstract class there is partial


abstraction.(0 to 100 %)

Q) Multiple inheritance is not supported through class in java, but it is possible


by an interface, why?
multiple inheritance is not supported in the case of class because of ambiguity.
However, it is supported in case of an interface because there is no ambiguity.
It is because its implementation is provided by the implementation class.

Example :

package multipleInheritancewrtInterface;

public interface Printable {

public abstract void print();


}

package multipleInheritancewrtInterface;

public interface Showable {

public abstract void show();


}
package multipleInheritancewrtInterface;

public class Test implements Printable, Showable {

@Override
public void show() {
[Link]("Show method");

@Override
public void print() {
[Link]("Print method");

public static void main(String[] args) {

Test t=new Test();


[Link]();
[Link]();
}
}

Example :

package multipleInheritancewrtInterface;

public interface Showable {

public abstract void show();


}

package multipleInheritancewrtInterface;

public interface Printable {

public abstract void show();


}

package multipleInheritancewrtInterface;

public class Test implements Printable, Showable {

@Override
public void show() {
[Link]("Show method");

public static void main(String[] args) {

Test t = new Test();


[Link]();

}
}

Common questions

Powered by AI

Abstract classes can contain constructors in Java because they act like regular classes with abstract features, allowing them to initialize state that can be shared by subclasses. On the other hand, interfaces are purely a specification, containing no state or behavior initialization capability, thus not permitting constructors .

Choosing an interface over an abstract class is ideal when you need to define a contract that multiple classes can adhere to without enforcing a specific inheritance chain. Interfaces provide maximum flexibility since a class can implement multiple interfaces, fostering decoupled architectures. This choice is beneficial for scenarios emphasizing polymorphism and system extensibility, such as APIs or frameworks, where implementation can vary widely .

In Java, interfaces (up to version 1.7) only allow abstract method declarations, and by default, these methods are public and abstract. In contrast, abstract classes can include both abstract and non-abstract methods and do not restrict modifiers on method declarations, allowing more flexibility in defining behavior .

The absence of multiple inheritance in classes is beneficial because it prevents complexity and ambiguity that arise from handling conflicting method implementations from various superclasses. This principle enhances code maintainability and clarity by enforcing a single inheritance path for concrete implementation. Interface-based multiple inheritance avoids these problems by delegating method implementation responsibility to the implementing class, maintaining a consistent and clear design pattern that supports polymorphism and code reuse .

Abstract classes facilitate polymorphism by allowing subclasses to inherit and optionally override common behavior while also being forced to implement abstract methods. Since abstract classes can have a mix of implemented and unimplemented methods, they serve as a flexible foundation for polymorphic behavior with shared code. Interfaces, however, offer polymorphism purely by behavioral contracts without any shared implementation, requiring every implementer to define methods independently, which can increase code size but ensures total behavioral flexibility and independence .

Java supports multiple inheritance through interfaces because interfaces only contain method declarations without any implemented body, and the implementation class provides the actual method definitions. This avoids ambiguity seen with classes, where a method might have multiple conflicting implementations. By implementing multiple interfaces, a class can still inherit multiple behaviors without encountering the typical conflicts associated with class-based multiple inheritance .

Interfaces in Java achieve 100% abstraction by only providing method declarations without any implementation, which forces implementing classes to define the method bodies. In contrast, abstract classes can have partial abstraction as they might contain constructors and implemented methods, allowing them to hold state and share common method implementations, which can dilute the abstraction . This complete abstraction is crucial in designing systems that require strict separation of interface and implementation, enhancing modularity and scalability.

In Java, using interfaces to define constants can create a poor design practice. While interface constants are public, static, and final by default, using interfaces for constants leads to an anti-pattern known as the 'constant interface' pattern. This practice breaks encapsulation and exposes implementation details, making the interface less flexible for future changes. Instead, it is advisable to use enums or dedicated class structures to hold constants .

Java interfaces support consistent API design by defining a clear contract that classes must adhere to, ensuring a uniform method signature and expected behavior. This promotes a separation of concerns as interfaces decouple the 'what' (the method signatures and return types) from the 'how' (implementation provided by the implementing classes). As a result, interfaces provide a robust framework for API consistency, allowing separate teams to develop implementations without impacting the defined contract, facilitating maintainable and scalable software solutions .

Encapsulation in abstract classes and interfaces differs significantly. Abstract classes can encapsulate data through variables and methods (both abstract and concrete), providing controlled access through access modifiers. Interfaces, however, provide only behavioral contracts and cannot encapsulate state directly due to the lack of variable definitions beyond public, static, and final fields . This structure limits interfaces to defining behaviors without managing an implementation or state.

You might also like