0% found this document useful (0 votes)
13 views2 pages

Inheritance and Association in OOP

The document explains various types of inheritance in object-oriented programming, including single, multilevel, hierarchical, multiple, and hybrid inheritance, along with the concept of interfaces. It also discusses the relationship between classes through association, aggregation, and composition, highlighting the differences in their relationships and lifecycles. Inheritance represents an IS-A relationship, while aggregation and composition represent HAS-A and PART OF relationships, respectively.

Uploaded by

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

Inheritance and Association in OOP

The document explains various types of inheritance in object-oriented programming, including single, multilevel, hierarchical, multiple, and hybrid inheritance, along with the concept of interfaces. It also discusses the relationship between classes through association, aggregation, and composition, highlighting the differences in their relationships and lifecycles. Inheritance represents an IS-A relationship, while aggregation and composition represent HAS-A and PART OF relationships, respectively.

Uploaded by

Praveen Karoshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Inheritance - Code reusability and Extensibility

In a class hierarchy - we call the derived class as sub-class and parent class is
called super class
Types of Inheritance -
a. Single inheritance - Only one derived class from a base class is called single
inheritance
b. Multilevel inheritance - More than one class getting derived in a herirachy, one
after the other is called multilevel inheritance
c. Heirarchical inheritance - More than one class is derived from a single parent
class
d. Multiple inheritance - is not through classes but through interfaces. A derived
class inherits from more than one parent class
e. Hybrid inheritance - also called diamond inheritance where two interfaces derive
from parent class and another child class derives from thees two interfaces

Why multiple inheritance is not there in Java?


When a method is derived from more than one parent class, the child class faces
ambiguity in calling the version of the method. THe child class does not know which
version of the method to call. Similarly, the data variable gets inherited twice
which is also ambiguous.

Interface-to-Class inheritance is not allowed, and it goes against the fundamental


principles of class-based inheritance.

Interface inheritance: An Interface can extend another interface.

Inheritance represents the IS-A relationship which is also known as the parent-
child relationship.

Association, Aggregation, Composition


Association - Any interaction between classes in terms of objects of different
types is association. When we model the real world, two or more classes will have
to interact or have relationship. You cannot have just one class.
Definition - Association is the cardinal concept in object-oriented programming
that describes the relationship between two independent classes. Association can be
viewed as describing a “uses-a” relationship where an object uses, or in any way
interacts with, another object. Association may be either unidirectional or
bidirectional and may exist in several forms, such as one-to-one, one-to-many,
many-to-one, and many-to-many.
One to one - Student and LibraryCard
One to Many - Library and Student
Many to One - Students to Department
Many to Many - Professors to Department

Inheritance is IS-A relationship


Aggregation and Composition are two types of Association
Aggregation is a relationship that comes under object-oriented programming,
classifying an instance of a class as “HAS-A.” There is containment. A class
contains another class.

It’s a form of association with weaker relationship strength, whereby the lifetime
of the contained object (part) is not controlled based on the lifetime of the
container object (whole).
The lifetime of contained object (part) is NOT DEPENDENT on the container object
(whole)
Example - Department contains the student object.
Aggregation is a type of association that represents a relationship where one class
is a collection or container of another class. It depicts a “has-a” relationship,
where the container object can exist independently of its contents, and the
contained objects can exist independently of the container.

Composition is "PART OF" relationship - The lifetime of contained object (part) is


DEPENDENT on the container object (whole)
Example - House and rooms - When house is destroyed, the rooms cannot exist. So
house and rooms is an exampe for Composition
Example - Engine and its parts - Engine has piston, valves, screws, bolts
Composition is a core concept in object-oriented programming that refers to the
relationship “part-of” between classes. It is a stronger form of association in
which the contained objects’ lifecycle is strongly associated with the container
object’s lifecycle.

Common questions

Powered by AI

In aggregation, objects have independent lifecycles—the container's existence doesn't dictate the lifespan of the contained object. For example, a Department may contain Student objects, yet Students can exist independently of the Department. Conversely, in composition, the lifecycle of the contained object is dependent on the container object. If the container is destroyed, the contained objects are too, as seen with a House and its Rooms; rooms cannot exist without the house they belong to .

The main difference between aggregation and composition lies in the dependency of the lifecycle of the contained object. In aggregation, the contained object can exist independently of the container object, as shown by the example of a department containing student objects, where students can exist independently of the department. Conversely, in composition, the contained object's lifecycle is tied to the container object; if the container is destroyed, so is the contained object, exemplifying the relationship between a house and its rooms .

Aggregation and composition signify different levels of dependency between class objects. Aggregation represents a weaker form of dependency where the contained object can exist independently of the container, such as a department and its students; if the department ceases, the students remain. Composition indicates a strong dependency, where the contained object's existence is bound to the container. For example, rooms in a house illustrate composition, as their existence ceases if the house is destroyed. These relationships convey how objects are structured and depend on each other in software design .

Interface inheritance in Java allows an interface to extend another interface and only declares method signatures without any implementation, fostering multiple inheritance-like behavior. Class inheritance, however, allows a class to inherit from another class and includes both the method signatures and their implementations. This difference in structure means Java can support interface inheritance to avoid method ambiguity inherent in multiple class inheritance, thereby maintaining clear and conflict-free inheritance structures .

Association in object-oriented programming describes the relationship between two independent classes, emphasizing interactions where one class uses or interacts with the other. This 'uses-a' relationship is broader than inheritance, which defines a strict 'is-a' relationship where a class derives from another class, inheriting its properties and behaviors. While inheritance is concerned with class hierarchy and shared behavior, association focuses on how classes cooperate and communicate, forming a network of interactions rather than vertical class structures .

Hierarchical inheritance ensures clarity in method calls and attribute inheritance by having multiple classes derive from a single parent, thus maintaining a single path for method and attribute inheritance. This configuration avoids method ambiguity and duplication issues faced in multiple inheritance, where classes inherit from multiple parents potentially leading to conflicts regarding which parent’s method or attribute to use. Consequently, hierarchical inheritance leverages a clear, unambiguous inheritance hierarchy, enhancing the reliability of the code .

The 'diamond problem' in multiple inheritance arises when a class inherits the same method from multiple parent classes, leading to ambiguity about which method implementation should be used. Java addresses this concern by not supporting multiple class inheritance directly but using interfaces instead. Interfaces allow classes to implement methods without direct lineage, thus avoiding the conflict of method resolution seen in the diamond problem, as interfaces do not include method implementations, only declarations .

The IS-A relationship in object-oriented programming refers to the inheritance hierarchy where a subclass (or derived class) is a type of its superclass (or parent class). This relationship indicates that instances of the subclass can be treated as instances of the superclass, supporting polymorphism and code reusability. It guides inheritance by ensuring that derived classes inherit all attributes and behaviors of their parent class, following a parent-child relationship pattern .

Multiple inheritance allows a class to inherit behaviors and attributes from more than one parent class, leading to potential issues like method ambiguity and duplicate data inheritance, which causes confusion about which versions to call in the child class. This differs from single inheritance, where a class inherits from only one parent, preventing such conflicts, and from hierarchical inheritance, where multiple classes derive from a single parent, maintaining clarity in method and attribute association. In Java, multiple inheritance is not permitted to avoid these ambiguities. Instead, Java uses interfaces to achieve a form of multiple inheritance, since interfaces only declare methods without actual implementations, reducing the risk of conflict .

Association relationships in object-oriented programming model the interactions and dependencies between classes similar to real-world interactions. 'One-to-one' associations illustrate relationships where a single instance of one class is related to a single instance of another, such as a Student using one LibraryCard. 'Many-to-one' involves multiple instances of one class linked to a single instance of another class, like multiple Students being associated with a single Department. These associations reflect how objects interact in the real world by depicting 'uses-a' relationships .

You might also like