Q.1 Java
Q.1 Java
In Java, abstract classes and interfaces serve distinct purposes, each with its own set of characteristics and use cases.
Abstract Class:
Definition: An abstract class is a class that cannot be instantiated and can contain both abstract and non-abstract
methods.
Method Implementation: Abstract classes can have a mix of implemented and abstract methods, allowing some
methods to have a defined behavior while others must be overridden by subclasses.
Inheritance: A class can inherit from only one abstract class due to the nature of representing a type of object.
Access Modifiers: Abstract classes can have various access modifiers like public, protected, and private for their
methods and properties.
Variables: Abstract classes can have member variables, including final, non-final, static, and non-static variables.
Interface:
Definition: An interface is a contract specifying a set of methods that a class must implement. It cannot be instantiated
and only contains method signatures.
Method Implementation: All methods in an interface are by default abstract and must be implemented by any class
that implements the interface.
Inheritance: A class can implement multiple interfaces but can inherit from only one abstract class. Interfaces
represent a set of behaviors.
Access Modifiers: Interfaces can only have public access for their methods.
Variables: Interfaces cannot have member variables; they only allow static and final variables.
In summary, while abstract classes are used as base classes for concrete subclasses to inherit from and can contain both
implemented and abstract methods, interfaces are used to define a set of methods that a class must implement and
achieve full abstraction. Abstract classes provide more flexibility with state and method implementation, whereas
interfaces offer strict contracts for behavior implementation.
2. An abstract class can have all four; static, non-static Only final and static variables are used.
and final, non-final variables.
3. To declare abstract class abstract keywords are The interface can be declared with the interface
used. keyword.
5. The keyword ‘extend’ is used to extend an abstract The keyword implement is used to implement
class the interface.
6. It has class members like private and protected, has class members public by default.
etc.
Definition:
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods (methods without
a body) alongside concrete methods (methods with a body).
An interface is a reference type, similar to a class, that can contain only constants, method signatures, default
methods, static methods, and nested types.
Instantiation:
Abstract classes cannot be instantiated directly; they need to be subclassed. Subclasses of abstract classes must
provide implementations for all abstract methods unless the subclass itself is declared as abstract.
Interfaces cannot be instantiated at all. They are implemented by classes, which provide concrete implementations for
all methods declared in the interface.
Multiple Inheritance:
In Java and some other languages, a class can only inherit from one abstract class (single inheritance), but it can
implement multiple interfaces (multiple inheritance). This allows for greater flexibility in designing class hierarchies.
Abstract classes can have constructors, instance variables, and non-abstract methods, providing a way to share code
among closely related classes. Interfaces cannot contain instance fields or constructors.
Usage:
Use abstract classes when you have a common base class with some default behavior that needs to be inherited by
subclasses, or when you want to enforce a common contract among closely related classes.
Use interfaces when you want to define a contract for a set of classes to adhere to, without specifying any
implementation details. Interfaces are especially useful when you need multiple classes to implement the same set of
methods but don't necessarily share a common implementation or inheritance hierarchy.