Abstraction
Abstraction: “To represent the essential feature without representing the background details”
Abstract Data Types (ADT)
-
Definition:
- A mathematical model for data types
- Defines a data type by its behavior from the user’s perspective
- A logical representation focusing on what operations are available
-
Key Characteristics:
- Defines operations that can be performed (the “what”)
- Hides implementation details (the “how”)
- Provides implementation-independent view
- Abstracts internal data organization
- Specifies behavior without specifying mechanism
-
Relationship with Data Structures:
- Data structures are concrete implementations of ADTs
- Same ADT can have multiple data structure implementations
- ADT focuses on abstract properties, data structures focus on concrete representation
-
Encapsulation:
- Separates specification from implementation
- Hides internal complexity
- Enables modularity in program design
- Allows implementation changes without affecting user code
-
Example:
- List ADT defines operations like insert, delete, search
- Can be implemented as Array List or Linked List
- Stack ADT defines push, pop operations
- Queue ADT defines enqueue, dequeue operations
-
Advantages:
- Abstraction: Users only see essential operations
- Conceptualization: Better models real-world scenarios
- Robust: Effective error handling
- Independence: Works with multiple data structures
- Information Hiding: Controls data access
-
Disadvantages:
- Overhead: Extra memory and processing costs
- Complexity: Harder to implement large structures
- Learning Curve: Takes time to master
- Cost: Needs extra development resources
- Limited Flexibility: May not fit all use cases
-
ADT:

-
The list of ADT Functions is given below:

3 Types of ADT
-
List ADT: The data is generally stored in key sequence in a list which has a head structure consisting of count, pointers and address of compare function needed to compare the data in the list. The data node contains the pointer to a data structure and a self-referential pointer which points to the next node in the list. Operations:
get(),insert(),remove(),removeAt(),replace(),size(),isEmpty(),isFull()

-
Stack ADT: In Stack ADT Implementation instead of data being stored in each node, the pointer to data is stored. The program allocates memory for the data and address is passed to the stack ADT. The head node and the data nodes are encapsulated in the ADT. The calling function can only see the pointer to the stack. Operations:
push(),pop(),peek(),size(),isEmpty(),isFull()
-
Queue ADT: Each node contains a void pointer to the data and the link pointer to the next element in the queue. The program’s responsibility is to allocate memory for storing the data. Operations:
enqueue(),dequeue(),peek(),size(),isEmpty(),isFull()
Difference Between ADTs and UDTs
| Aspect | Abstract Data Types (ADTs) | User-Defined Data Types (UDTs) |
|---|---|---|
| Focus | Abstraction of data and operations | Custom data types created by programmers |
| Purpose | Provides a high-level conceptual view of data structures and functionalities | Models specific data structures based on primitive types or other UDTs |
| Implementation Details | Does not specify how operations are implemented or how data is structured | Specifies how to create and organize data types to implement the structure |
| Usage | Used to design and conceptualize data structures | Used to implement data structures that realize the abstract concepts defined by ADTs |
| Example | List ADT, Stack ADT, Queue ADT | Structures, classes, enumerations, records |
Abstract Classes
-
Declared using the
abstractkeyword before the class declaration -
Purpose of abstract class is only to achieve polymorphism and inheritance by other classes
-
Cannot instantiate objects directly
-
Can have abstract and non-abstract methods
-
Can declare references of abstract class type
-
Example:
public abstract class Shape { ... } -
Declaration:
- Declared using the
abstractkeyword before the class declaration - Example:
public abstract class Shape { ... }
- Declared using the
-
Purpose:
- Provides a base class with common functionality
- Enables code reuse through inheritance
- Achieves polymorphism
- Defines a common interface for related classes
- (The original point was incorrect - abstract classes serve more purposes than just polymorphism)
-
Instantiation:
- Cannot be instantiated directly
- Can declare references of abstract class type
- Must be extended by concrete classes to be used
-
Methods:
- Can have both abstract and non-abstract methods
- Abstract methods must be declared with abstract keyword
- Can provide default implementations for some methods
- Concrete classes must implement all abstract methods
-
Additional Features:
- Can have constructors and instance initializers
- Can have instance variables (fields)
- Can have static methods and variables
- Can have access modifiers for members
- Can implement interfaces
Abstract Class v/s Interface
| Comparison Point | Abstract Class | Interface |
|---|---|---|
| Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default |
| Implementation Method | Can have both implemented and abstract methods | Methods are abstract by default; Java 8 can have default and static methods |
| Inheritance | A class can inherit from only one abstract class | A class can implement multiple interfaces |
| Access Modifiers | Methods and properties can have any access modifier (public, protected, private) | Methods and properties are implicitly public |
| Variables | Can have member variables (final, non-final, static, non-static) | Variables are implicitly public, static, and final (constants) |
| Keyword Used | The abstract keyword is used to declare abstract class | The interface keyword is used to declare interface |
| Usage | when you have a group of closely related classes that share common functionality and fields | when you want unrelated classes to be able to perform a specific action, regardless of their inheritance hierarchy |
| Example | an Animal abstract class can have subclasses like Cat and Dog with shared fields like age and name | an AnimalStuff interface with a poop method can be implemented by both Animal subclasses and other classes that are not animals |
Abstract Methods
- Methods without a body (implementation)
- Declared with abstract keyword
- End with semicolon instead of curly braces
abstract void methodTwo(); - Can declare references of abstract class type:
Test t; // Allowed - Cannot create objects of abstract class:
Test t = new Test(); // Not allowed - Abstract Class Cannot Be Final: Final prevents inheritance. Abstract classes are meant to be inherited. Therefore, abstract and final cannot be used together
- Abstract Class Cannot Be Static: Static classes are typically nested classes. Abstract classes cannot be declared static
- Abstraction Function: A function from an object’s concrete representation to the abstract value it represents.

Concrete Class
- Regular classes without the
abstractkeyword - Can instantiate objects
- Can have references
- Must implement all inherited abstract methods