0% found this document useful (0 votes)
5 views20 pages

3

Inheritance is a mechanism in object-oriented programming that allows a class (subclass) to inherit properties and behaviors from another class (superclass), enabling code reuse and extension. Subclasses can add new methods, override inherited methods, and maintain type equivalence with their superclasses. Multiple inheritance allows a subclass to inherit from multiple superclasses, but it can lead to naming conflicts, which some languages like Java do not support.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

3

Inheritance is a mechanism in object-oriented programming that allows a class (subclass) to inherit properties and behaviors from another class (superclass), enabling code reuse and extension. Subclasses can add new methods, override inherited methods, and maintain type equivalence with their superclasses. Multiple inheritance allows a subclass to inherit from multiple superclasses, but it can lead to naming conflicts, which some languages like Java do not support.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Inheritance

What is Inheritance?

• Classes can be defined in terms of other classes


– For example, we can define classes Maruti and
Santro, they can be defined in terms of the class
car
• Maruti: subclass of Car
• Car: superclass of Maruti
Superclass Car

Subclassses Maruti Santro Merc


What is Inheritance? (Cont..)

• Inheritance is the mechanism which allows a class


to inherit properties of another class
• Each subclass inherits variables and methods from
the super-class
• subclasses can extend the behavior of superclass by
adding new variables and new methods
• Subclasses can also override inherited methods
and provide specialized implementations for
those methods
• Inheritance is not limited to just one layer, the
inheritance tree(class hierarchy) can be as deep
as needed
What is Inheritance? (Cont..)

• Creating new class by extending or inheriting


from an old class
• All members of old class are available
• The interface of the old class is also applicable to
the new class
• The new class supports the interface of the old
class
• So, it can respond to messages destined for
objects of the old class
• Therefore, we say they are type-equivalent
What is Inheritance? (Cont..)

• Base class Vs Derived class


• By default, the derived class inherits both the
implementation & the interface
– i.e., the state variables, methods
• Differentiating derived class from base class
– Extending base class: Adding brand new methods
– Overriding: Changing behavior of base class:
selectively modify certain methods of the interface
• To Override a base class function
– Creating a new definition in the derived class for
the function
Why we need Inheritance?

• Need to create new datatypes (classes) similar to


existing ones
• Creating these new classes from scratch is
painful
• Need for mechanisms to selectively modify the
behavior of original class and add new
functionality
Benefits of Inheritance

• Subclasses provide specialized behaviors from the


basis of common elements provided by the supe-
rclass
• Through the use of inheritance, programmers can
reuse the code in the super-class many times

• Abstract Class
– define generic behavior
– the abstract super-class defines and may partially
implement the behavior but much of the class is
undefined and unimplemented
– the details are filled in with specialized sub-classes
Inheritance for Conceptually Compatible Classes
• Contract Conformance (Conceptual Inheritance)
• Extension
• Refinement.

Is Kind Of Relationship
Subclass (Derived Class)
Superclass (Base Class)
Inheritance for Pure Extension

• base = {f1(),f2(),f3()}

• derived = base + {f4(),f5()}

• Example:

• basestream={read,write,close}

• derivedstream={read,write,close,seek}
Inheritance for Refinement
• base = {f1(),f2(),f3()}

• derived = {f1(),f2(),f3()} with different behavior

• Example:
– baseStream={read,write,close}

– derivedSafelySharableStream={read,write,close}
automatically locks the stream during an
operation
What Happens to Implementation?
• Inherited method bodies: available as they are,
or replaceable through refinements

• Private Members: Not accessible, but available for


the sake of method bodies that are ’inherited’

• Protected Members: Accessible and


Communication between superclass’s member
functions and subclass’s member functions can
take place through these
Is Conceptual Compatibility Enforced?

• The models of inheritance in OOPLs do not


enforce conceptual compatibility between a
subclass and its superclass
Multiple Inheritance
• A class can have more than one super-classes
• This enables the sub-class to inherit properties of
more than one super-class and to merge their
properties
• Example
– we have two classes - Land-vehicle and Water-
vehicle
– now we define a class Amphibious-vehicle that has
properties of both an Land-vehicle and Water-
vehicle Land V Water V

Amphi V
Multiple Inheritance (Cont..)

• Multiple inheritance may produce naming-


conflicts, if at least two of the super-classes
define properties with the same name
A B

int num; float num;

• Java does not support Multiple Inheritance.


Multiple Inheritance: Issues

InterestbearingItem Asset Insurable Item

Security Bankaccount RealEstate

Bond Stock Checkingaccount Savingaccount

MutualFund
Decomposition (HAS-A) Hierarchy

• Personal Computer is composed of


– CPU
– Memory
– Keyboard
– HDD
– ......
• This is Decomposition Hierarchy
Why do we need hierarchy?

• The Limitation of the Human Capacity for Dealing


with Complexity
• Maximum number of chunks of information that an
individual can simultaneously comprehend is on the
order of seven, plus or minus two
• Human chunking capacity is related to the capacity
of short-term memory
• Processing speed is limiting factor - it takes the
mind about five second to accept a new chunk of
information
Why do we need hierarchy? (Cont..)

• Group logically related abstractions


• Minimize dependency among modules
• Simple enough to understand fully
• Ease of Change
– Possible to change the implementation of one modules
without affecting the behaviour of the other modules
– The ease of making a change in the design should bear
a reasonable relationship to the likelihood of the
change being needed
Decomposition of software systems

• Algorithmic decomposition
– System is described as a sequence of steps
(algorithm)
– Each module denotes a major step in the process
– Also known as top-down structured decomposition
• Object Oriented decomposition
– View the world (problem domain) as a set of
autonomous agents that collaborate
– Each Agent “worries” about his job.
– Provides a standard set of services
– May use services of others agents
– Interaction among agents is “Well-defined”.i.e., they
follow a protocol
Algorithm Versus Object-oriented Decomposition
Algorithmic Object -Oriented
• Emphasizes the agents that either (1)
• Highlights the ordering of Cause action (Clients) or (2) Are the
events subjects on which these operations act
• Typically larger systems (Servers)
• Lower reuse •Yields smaller systems through the reuse
•Less resilient and more risky of common mechanisms
to build complex system with •Leverages high reuse
this decomposition. •More resilient to change due to
•Typically flat and unable to underlying stable intermediate forms
reduce complexity •Reduces the risk of buildings complex
•Computing Model is typically software systems because they evolve
von Neumann incrementally (iterative refinement)
•Low concurrency •Directly address the inherent complexity
of software by using the separation of
concerns in a large state space
•Computing Model is Client-Server
•Inherently distributed; High concurrency

You might also like