Session Id 22012641012
docx
keyboard_arrow_up
School
University of New South Wales *
*We aren’t endorsed by this school
Course
485
Subject
Computer Science
Date
Nov 24, 2024
Type
docx
Pages
13
Uploaded by ChancellorElectron10813
Topic: The role of inheritance in software design
Introduction:
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows the creation
and organization of classes and objects. It enables the reuse of code, promotes modularity, and
facilitates extensibility in software design. Inheritance allows a class to inherit properties and
behaviors from other classes, thereby establishing a hierarchical relationship between them. This
hierarchical structure facilitates the creation of more specialized classes, which can use and extend
the functionality of their parent classes.
In the realm of computer science,
inheritance in object-oriented programming (OOPS) plays a
vital role in structuring and organising code efficiently. As a
fundamental concept in software development, inheritance
enables the creation of new classes that inherit attributes
and behaviours from existing classes, improving code
reusability and overall efficiency. This article will provide
you with a thorough understanding of the definition of
inheritance in OOPS, its key concepts, advantages, and
importance. Moreover, you will explore different types of
inheritance with examples and learn about implementation
strategies in various
programming languages
. Ultimately,
this knowledge will help you effectively apply inheritance to
real-world programming scenarios and create robust,
maintainable software solutions.
A key feature of the object-oriented (OO)
paradigm is that of inheritance [21]. Use of inheritance is claimed to reduce the amount of software
maintenance necessary, ease the burden of testing [1], and produce more reliable, high quality
software [2]. In this paper, we investigate five OO systems, all written in C++, and empirically evaluate
hem using a subset of Chidamber and Kemerer's (C&K) OO design metrics [11], together with two
further inheritance-based design metrics developed as part of the MOOPS project [17][18]. The
results of our analysis (and that of other investigations) seem to support the thesis that inheritance is
either used sparingly during the development process, or is used incorrectly [5][9]. Use of
inheritance does not seem to be delivering the benefits it promised, and it is not clear that systems
using inheritance are easier to maintain than those that do not. In this paper, an analysis of the faults
found in three of the five systems investigated showed very little relationship with any of the
inheritance-based metrics. Inheritance-based metrics collected for all five systems also show a lack of
relationship with three of the dependent variables collected for the five systems (i.e., the number of
non-comment source lines, the number of known errors and error density). However, an interesting
relationship was found between the depth of the inheritance tree and software understandability. In
the next section, we describe related work in this area. In Section 3, we describe the five application
domains studied and the metrics collected for each. Section 4 presents summary data for each of the
five systems studied. Correlations and data analyses of each system are presented in Section 5,
incorporating analyses of fault data in three of the systems. The implications of our results are then
described (Section 6). Finally, some conclusions and suggestions for further work are presented
(Section 7).
Research Background:
Inheritance has been widely used in software design for decades, particularly in languages such as
Java and C++. It is a core principle of OOP and has been instrumental in the development of large-
scale software systems. However, despite its widespread use, there is ongoing debate about the
appropriate use of inheritance and its impact on software design. There are concerns about the
potential pitfalls of inheritance, such as code duplication, tight coupling, and the inflexible class
hierarchies it can create. Various empirical investigations have been made into the use of
inheritance.
In OO systems, inheritance can be considered to be a form of coupling, in that to understand the
functionality of one class, other related classes may have to be understood as well; this adds a level
of structural and cognitive complexity to a system. Hence, classes at the bottom of a deep
inheritance hierarchy would be more difficult to maintain than those higher up [13]. From a
maintenance point of view, this would seem to condone either having zero or relatively low amounts
of inheritance in systems, or inheritance in the form of a number of small, disjoint subtrees. We find
support for this in [3], which hypothesises that a system with a large, single inheritance structure is
more fault-prone than a system comprising many independent subtrees. The shapes of forests of
inheritance trees can also affect the amount of code reuse in an object-oriented system [5]. Our
results from Systems 1 and 2 show that understandability (and, by implication, maintainability)
decreases as DIT increases. A possible reason for the confusion over the use of inheritance is that
inheritance itself is not an easy concept to learn and use well; the lack of use of inheritance may
result from a fear of the effects of using it. For example, in C++, the distinction between public,
private and protected inheritance is subtle; such idiosyncrasies add to the complexity of learning to
use inheritance, perhaps discouraging its use during design and implementation of OO systems. In
[13], common abuses of inheritance are described. These can be attributed to a lack of experience
on the part of the programmer. Use of C++ friends, as a simple alternative to inheritance may also
explain the lack of inheritance in C++ systems. Friends can be used to change the functionality of a
system, after it has been written, without major changes to the structure of the design or resulting
code. This would be indicative of a faulty design and potential maintenance problems [12]. In
addition, by violating encapsulation, friends are considered to be bad programming practice [8]. In
proposing the use of various OO metrics, [19] suggest an upper limit of zero on friends because of
their potential for harming the structure of a system. It could also be argued that inheritance falls
into the class of programming language features which are accepted and instigated without thorough
analysis, only to be subsequently rejected when experience has shown them to be unhelpful. It
would appear that encapsulation is more relevant and applicable than inheritance, and its benefits to
the maintenance process are more obvious. In [12], it is suggested that in future systems,
architectures based on aggregation will be more appropriate than those based on inheritance. This is
particularly true of systems incorporating multiple inheritance whose structures have tended to be
elaborately concocted. Interestingly, the NOC metric showed no significant correlations in any of the
tables.
7. Conclusions and further work There is clearly a need for research to address many urgent issues
arising from the use of inheritance and its effect on the process of maintenance. Fundamental issues
such as whether inheritance can make maintenance of OO systems easier, whether there is an
optimum level of inheritance, and whether we should focus on alternative features as a means of
reducing the maintenance burden all need be addressed. Ideally, this empirical research should be
carried out on as many real-world systems as possible, (with subjects of varying experience),
supported by well-designed hypotheses. Industrial-strength tools need to be provided to aid the
speedy collection of data and dissemination of results.
Research Aim:
This research aims to explore the role of inheritance in software design and its impact on code reuse,
modularity, and extendability. It seeks to provide insights and guidelines for the appropriate use of
inheritance in software projects, considering its advantages and potential pitfalls.
Research Questions:
1. What are the benefits of using inheritance in software design?
2. What are the potential pitfalls and limitations of inheritance in software design?
3. How does inheritance affect code reuse, modularity, and extendability?
4. How can inheritance be used effectively in software design to avoid code duplication and tight
coupling?
5. What are the alternative design patterns or techniques that can be used instead of inheritance?
Literature Review:
The role of inheritance in software design has been a topic of discussion and research in the field of
software engineering for many years. Numerous studies and articles have explored the benefits,
limitations, and best practices associated with inheritance.
In terms of the benefits of using inheritance, research has highlighted its ability to promote code
reuse and reduce development time. Inheritance allows developers to create new classes by
extending existing ones, inheriting their properties and behaviors. This reuse of code can lead to
improved productivity, as developers do not have to start from scratch when creating new classes.
Additionally, inheritance supports the principle of modularity, as it allows for the organization of
code into smaller, more manageable units.
However, there are also potential pitfalls and limitations associated with inheritance. One common
concern is code duplication, where similar code is duplicated across multiple classes in a class
hierarchy. This can make the codebase harder to maintain, as any changes made to the duplicated
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
code need to be applied in multiple places. Another concern is tight coupling, where changes made
to a parent class can have unintended consequences on its subclasses. This can lead to issues with
code maintenance and scalability.
Additionally, the use of inheritance can result in inflexible class hierarchies, making it difficult to
modify or extend the functionality of existing classes. This can limit the adaptability of the software
system and make it harder to accommodate future changes.
To address these concerns, researchers and practitioners have proposed alternative design patterns
and techniques that can be used instead of or in conjunction with inheritance. Some of these
alternatives include composition, interface-based programming, and the use of design patterns such
as the Strategy pattern. These alternatives aim to provide more flexibility, decoupling, and
modularity in software design.
Definition of Inheritance in
Oops
Inheritance is a fundamental concept of Object-Oriented
Programming (OOP) that enables a new class to inherit the
properties and methods of an existing class. This feature
promotes code reusability and creates a parent-child
relationship between classes.
Key Concepts of Inheritance in Object-
Oriented Programming
Inheritance: Inheritance is an OOP mechanism allowing a
new class (the derived or child class) to acquire the
properties and methods of an existing class (the base or
parent class).
When designing a program using inheritance, it is essential
to understand some key concepts:
Base Class (Parent Class)
: This is the existing class
from which a new class inherits properties and
methods. The base class is also known as the parent
or superclass.
Derived Class (Child Class)
: The new class that
inherits the properties and methods of the base class
is called the derived or child class. The derived class
can also add its new methods and properties.
Single Inheritance
: This occurs when a class only
inherits from one base class. This is the most common
type of inheritance used in OOP.
Multiple Inheritance
: In some
programming
languages
like C++ and
Python
, a class can inherit
from more than one base class. This approach is called
multiple inheritance.
Polymorphism
: Polymorphism is the ability of one
function or method to perform different actions
depending on the input or the type of object it is called
upon. Inheritance enables polymorphism by allowing
child classes to override or extend the functionality of
parent class methods.
Access Modifiers
: Access modifiers determine the
visibility and accessibility of class members (i.e.,
properties and methods) in an inheritance hierarchy.
Common access modifiers are public, private, and
protected.
Understanding the Parent-Child
Relationship in Inheritance
A crucial aspect of inheritance in OOP is the relationship
between the base class (parent) and the derived class
(child).
In OOP, inheritance follows the "is-a" relationship, meaning
that the derived class is a more specific version of the base
class. For example, in a class hierarchy for vehicles, a "Car"
class can inherit from a more general "Vehicle" class, as a
car is a type of vehicle.
There are various benefits of using the parent-child
relationship in inheritance, including:
Code Reusability
: Inheritance allows the derived
class to reuse the code from the base class, reducing
duplication and promoting maintainability.
Code Organization
: Organizing your code into a
hierarchy of classes with inheritance can make it
easier to understand the relationships and
dependencies between different parts of your
program.
Modularity
: With inheritance, you can modify or
extend the functionality of a base class without
changing the base class itself. This modularity can
enhance the flexibility and scalability of your code.
Let's consider an example to illustrate how inheritance
works in OOP. Suppose we have a parent class "Animal"
with properties like "name" and "age" and methods such as
"eat" and "sleep". We can create a derived class "Dog" that
inherits these properties and methods from the "Animal"
class. Additionally, the "Dog" class can add new methods
like "bark" and "fetch" to enhance its functionality.
In conclusion, inheritance is a powerful OOP concept that
allows classes to inherit properties and methods from other
classes, promoting code reusability and enhancing the
structure and organization of a program. Understanding the
key concepts of inheritance and the parent-child
relationship will help you design and develop more efficient
and maintainable software applications.
Advantages of Inheritance in
OOP
Inheritance in OOP provides several advantages that lead
to more effective and efficient software programs. The
most significant benefits include code reusability and
efficiency, as well as ease of maintenance and
modification.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Importance of Inheritance in
OOP
Inheritance is a cornerstone concept in Object-Oriented
Programming (OOP) that significantly contributes to the
creation oforganized, modular, and maintainable software
programs. Through parent-child relationships between
classes and an organized class hierarchy, inheritance
drives the benefits of modularity and organization while
also enabling other OOP features like polymorphism for
more efficient and adaptable code.
Promoting Modularity and Organization
Modularity and organization are essential for creating
efficient, maintainable, and flexible software applications.
Inheritance plays a key role in promoting these features by
enabling developers to build class hierarchies and manage
code in a more organized manner. Here's how inheritance
contributes to modularity and organization in OOP:
Class hierarchy management
: With inheritance,
developers can build a well-structured and interactive
class hierarchy based on specific classes' common
properties and methods. This hierarchical approach
organizes the code, making it easier to understand
and maintain.
Code modularization
: Inheritance facilitates the
separation of generic properties and methods in the
base class from the unique functionality of each
derived class. This modular approach leads to more
maintainable and scalable code as developers can
make changes to individual classes without affecting
the overall system.
Increased adaptability
: An organized and modular
code structure is more adaptable to changing
requirements. Inheritance encourages developers to
create flexible software that can be easily updated or
extended with new features.
Easier code navigation
: A well-structured class
hierarchy, facilitated by inheritance, simplifies code
navigation. Developers can quickly trace
dependencies and relationships between classes,
saving time and effort while
debugging
and refining
the program.
Facilitating Polymorphism in Object-
Oriented Programming
Polymorphism is another crucial OOP concept that enables
a single function or method to operate on different data
types or objects, leading to more flexible code and simpler
interfaces. Inheritance plays a vital role in facilitating
polymorphism in OOP by allowing derived classes to
override or extend the functionality of their parent classes'
methods. Here's how inheritance supports polymorphism:
Encapsulating functionality
: Inheritance
encapsulates the functionality within a base class,
while derived classes can utilize or modify this
functionality. This encapsulation enables objects of
different classes to share a common method interface.
Method overriding
: Inheritance allows derived
classes to override the methods of their parent class,
providing alternative or more specific functionality.
This feature enables the implementation of
polymorphism, as different classes can use a common
method interface with their implementation.
Reducing code complexity
: By leveraging
inheritance and polymorphism, developers can create
cleaner and less complex code. Instead of writing
multiple functions or methods for each data type or
class, a single method can handle multiple data types,
objects, or class instances.
Customizability
: Polymorphism makes it easier for
developers to customize individual classes in the
inheritance hierarchy without affecting the parent or
other derived classes. This customizability leads to
software that is more adaptable to a wide range of
scenarios and requirements.
In conclusion, inheritance holds great importance in OOP as
it promotes modularity and organization within software
programs. It also facilitates polymorphism, which enables
more flexible and efficient code. By understanding the vital
role of inheritance in OOP and leveraging its capabilities,
developers can create more efficient, maintainable, and
versatile software applications.
Types of Inheritance in OOP
with Examples
Inheritance is a powerful feature in Object-Oriented
Programming (OOP), and it comes in various forms.
Depending on the programming language and the specific
scenario, different types of inheritance can be used to
create organized, efficient, and maintainable software
applications.
Single, Multiple, and Multilevel
Inheritance
These types of inheritance are widely used in OOP, each
offering unique solutions for organizing and structuring
classes.
Single Inheritance
: In this type, a derived class
inherits from only one base class. It is the most
common and simplest form of inheritance and is
supported by all major programming languages
like
Java
, C#, and
Python
.
For example, you can create a "Vehicle" base class
and a "Car" derived class that inherits from the
"Vehicle" class. The "Car" class would inherit
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
properties and methods of the "Vehicle" class and can
also add specific properties and methods of its own.
Multiple Inheritance
: In multiple inheritance, a
derived class can inherit from more than one base
class at the same time. It is supported by some
programming languages like C++ and
Python
but not
in others like
Java
and C#.
As an example, you can create a "Robot" class that
inherits properties and methods from two separate
base classes: "Machine" and "ArtificialIntelligence".
Multilevel Inheritance
: When a derived class
inherits from a base class, which in turn inherits from
another base class, it is called multilevel inheritance.
This type of inheritance creates a multilevel class
hierarchy, allowing the properties and methods from
multiple parent classes to be inherited in a single
derived class.
Consider the example of a class hierarchy involving
the "Animal" class, a "Mammal" class derived from the
"Animal" class, and finally a "Dog" class derived from
the "Mammal" class. The "Dog" class would inherit
properties and methods from both the "Animal" and
the "Mammal" classes.
Hierarchical and Hybrid Inheritance
These types of inheritance are used to create complex
class hierarchies that cater to more advanced scenarios.
Hierarchical Inheritance
: In this type, multiple
derived classes inherit properties and methods from a
single base class. This type of inheritance is helpful
when you need to create several classes that share
some common functionality but also have unique
characteristics.
For example, a "Shape" base class can include
properties like "colour" and "size", and multiple
derived classes such as "Circle", "Rectangle", and
"Triangle" can inherit those common properties while
adding additional unique properties or methods
relevant to each distinct shape.
Hybrid Inheritance
: Hybrid inheritance is a
combination of two or more types of inheritance, such
as single, multiple, multilevel, or hierarchical
inheritance. A class hierarchy that involves hybrid
inheritance is often more complex but offers greater
flexibility and adaptability for creating customized
class structures.
An example of hybrid inheritance could involve a
"Person" base class with derived classes "Student" and
"Teacher", which then further inherit from another
base class, "Employee". This complex structure
combines hierarchical and multiple inheritances to
create a versatile class hierarchy catering to specific
needs.
Understanding and selecting the right type of inheritance
for a given scenario can greatly improve the organization,
efficiency, and maintainability of a software application.
The choice of inheritance type ultimately depends on the
programming language and specific requirements of the
software program being developed.
Implementing Inheritance in Different
Programming Languages
Inheritance is implemented differently depending on the
programming language you are working with. We will now
explore how inheritance is implemented in three popular
programming languages: Java, C++, and Python.
Java:
In
Java, the 'extends' keyword is used to denote inheritance
between classes. The properties and methods of the parent
class are made accessible to the child class through the
'extends' keyword unless they are marked as private.
class Animal { void makeSound()
{ System.out.println("Some sound"); } }
class Dog extends Animal { void bark()
{ System.out.println("Bark"); } }
In this example, the 'Dog' class inherits the 'makeSound'
method from the 'Animal' class, and it also has its own
method called 'bark'.
C++:
Inheritance in C++ is achieved
using the colon symbol (:) followed by an access specifier
and the name of the base class.
class Animal { public: void makeSound() { cout << "Some
sound" << endl; } };
class Dog : public Animal { public: void bark() { cout <<
"Bark" << endl; } };
In this C++ example, the 'Dog' class inherits the
'makeSound' method from the 'Animal' class and has its
own method called 'bark'. The public access specifier
defines how the inherited members should be accessible in
the derived class.
Python:
In Python, inheritance is
denoted using parentheses when defining the derived
class. The parent class is placed inside the parentheses,
and the child class inherits its properties and methods.
class Animal: def make_sound(self):
print("Some sound")
class Dog(Animal): def bark(self):
print("Bark")
In this Python example, the 'Dog' class inherits the
'make_sound' method from the 'Animal' class and
implements a new method called 'bark'.
Real-World Examples and Applications
Inheritance in OOP is widely used in real-world software
applications due to its benefits of code reusability,
organization, and modularity. Here are a few examples
illustrating inheritance in practical use-cases: 1.
Graphic
User Interface (GUI) Frameworks:
In GUI frameworks,
inheritance is used to define the hierarchy of user interface
elements. For instance, a base 'Widget' class can define
properties like position, size, and visibility, while derived
classes like 'Button', 'TextBox', and 'Label' can inherit and
extend these properties with their specific attributes and
behaviours. 2.
Game Development:
In game
development, inheritance can be used to create a hierarchy
of game objects or characters. For example, a parent
'GameObject' class might contain properties like position
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
and velocity, while derived classes like 'Player', 'Enemy', or
'PowerUp' would inherit the common properties and extend
them with their unique attributes and behaviours.
3.
Content Management Systems:
Content
management systems often employ inheritance to create a
flexible system for handling various types of content. For
example, a basic 'ContentItem' class can define properties
like title, author, and publication date, while more specific
derived classes like 'Article', 'Image', 'Video', and 'Audio'
can inherit these shared properties while adding their
unique attributes. These examples emphasize the
importance of inheritance in real-world software
applications, showcasing its capability to promote code
reusability and organization while simplifying the
development process.
In conclusion, the role of inheritance in software design is a complex and multifaceted topic. While
inheritance can provide benefits such as code reuse and modularity, its use requires careful
consideration to avoid potential pitfalls such as code duplication and tight coupling. This research
aims to provide insights and guidelines for the appropriate use of inheritance in software projects,
considering its advantages and potential limitations. By examining the existing literature and
conducting empirical studies, this research aims to contribute to the ongoing discussion
Related Documents
Recommended textbooks for you
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,