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

Class3

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Class3

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like