What is an abstract class? Explain concepts of aggregation, generalization and specialization with the help of suitable example. [MCS-219, Q5]

Abstract Class: An abstract class is a class that cannot be instantiated (object cannot be created) and is used as a base class for other classes.
It may contain abstract methods (methods without implementation) as well as concrete methods (methods with implementation).

Key Points:

  • Used to represent common behavior
  • Acts as a template for derived classes
  • Forces subclasses to implement abstract methods

Example (Conceptual):

abstract class Shape {
    abstract void draw();   // abstract method

    void display() {
        System.out.println("This is a shape");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

Here, Shape is an abstract class, and Circle provides implementation of draw().


Aggregation: Aggregation represents a “has-a” relationship where the part can exist independently of the whole.

Characteristics:

  • Weak association
  • Objects have independent lifecycles
  • Represented in UML by a hollow diamond (◇)

Example:

Department – Teacher

  • A department has teachers
  • Teachers can exist even if the department is closed
Department ◇──── Teacher

Another Example:

Library – Book

  • A library has books
  • Books can exist without a library

3. Generalization

Generalization represents an “is-a” relationship where a common superclass is created from similar subclasses.

Characteristics:

  • Bottom-up approach
  • Common attributes and methods are moved to the parent class
  • Represented by a triangle arrow (△) pointing to the parent

Example:

Vehicle as a generalized class

        Vehicle
           △
     ----------------
     |              |
   Car           Bike

4. Specialization

Specialization is the reverse of generalization where new subclasses are created from a general class by adding specific features.

Characteristics:

  • Top-down approach
  • Child classes have more specific properties
  • Also uses inheritance

Example:

Employee → Manager, Clerk

Manager and Clerk are specialized forms of Employee

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *