Skip to Content
bytegarden

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:
    ADT

  • The list of ADT Functions is given below:
    Functions of ADT

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()
    List ADT

  • 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()

    Stack ADT

  • 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()

    Queue ADT

Difference Between ADTs and UDTs

AspectAbstract Data Types (ADTs)User-Defined Data Types (UDTs)
FocusAbstraction of data and operationsCustom data types created by programmers
PurposeProvides a high-level conceptual view of data structures and functionalitiesModels specific data structures based on primitive types or other UDTs
Implementation DetailsDoes not specify how operations are implemented or how data is structuredSpecifies how to create and organize data types to implement the structure
UsageUsed to design and conceptualize data structuresUsed to implement data structures that realize the abstract concepts defined by ADTs
ExampleList ADT, Stack ADT, Queue ADTStructures, classes, enumerations, records

Abstract Classes

  • Declared using the abstract keyword 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 abstract keyword before the class declaration
    • Example: public abstract class Shape { ... }
  • 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 PointAbstract ClassInterface
DefinitionCannot 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 MethodCan have both implemented and abstract methodsMethods are abstract by default; Java 8 can have default and static methods
InheritanceA class can inherit from only one abstract classA class can implement multiple interfaces
Access ModifiersMethods and properties can have any access modifier (public, protected, private)Methods and properties are implicitly public
VariablesCan have member variables (final, non-final, static, non-static)Variables are implicitly public, static, and final (constants)
Keyword UsedThe abstract keyword is used to declare abstract classThe interface keyword is used to declare interface
Usagewhen you have a group of closely related classes that share common functionality and fieldswhen you want unrelated classes to be able to perform a specific action, regardless of their inheritance hierarchy
Examplean Animal abstract class can have subclasses like Cat and Dog with shared fields like age and namean 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. Abstraction Function

Concrete Class

  • Regular classes without the abstract keyword
  • Can instantiate objects
  • Can have references
  • Must implement all inherited abstract methods