0% found this document useful (0 votes)
17 views

Q.1 Java

An abstract class is a base class that cannot be instantiated and contains both abstract and concrete methods, while an interface contains only abstract method signatures and defines a contract for implementing classes to adhere to. Abstract classes allow for inheritance and can contain implemented methods and variables, whereas interfaces only support multiple inheritance and provide strict behavior contracts without implementation details. In summary, abstract classes are used as base classes for inheritance and sharing code, while interfaces define common behaviors without enforcing a shared inheritance structure.

Uploaded by

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

Q.1 Java

An abstract class is a base class that cannot be instantiated and contains both abstract and concrete methods, while an interface contains only abstract method signatures and defines a contract for implementing classes to adhere to. Abstract classes allow for inheritance and can contain implemented methods and variables, whereas interfaces only support multiple inheritance and provide strict behavior contracts without implementation details. In summary, abstract classes are used as base classes for inheritance and sharing code, while interfaces define common behaviors without enforcing a shared inheritance structure.

Uploaded by

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

Difference Between Abstract Class and Interface

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.

S.No. Abstract Class Interface


1. An abstract class can contain both abstract and Interface contains only abstract methods.
non-abstract methods.

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.

4. It supports multiple inheritance. It does not support multiple inheritance.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy