0% found this document useful (0 votes)
4 views13 pages

Understanding Inheritance in Programming

Uploaded by

mani.nagpure070
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)
4 views13 pages

Understanding Inheritance in Programming

Uploaded by

mani.nagpure070
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

Inheritance

1. Similar to Inheritance in Life.


2. Child inherits attributes and behaviours from Parent.
3. Inheritance is a way to create a Class from existing Class.
4. The Derived / Child / Sub class inherits some attributes and behaviours from Base / Parent /
Super Class, and may have more specialised attributes and behaviours.
When do we use Inheritance?
1. After grasping the definition, the pivotal question arises: "When do we use inheritance?”
2. Use inheritance whenever an IS-A relationship is identified between objects.
3. Inheritance is applied where objects exhibit a hierarchical relationship, signifying a
specialized-generalized connection.
4. Do not Reinvent the Wheel.
General “is-a” Examples
Vehicle
un
.
Animal

G Truck

Bird Reptile
-
.
ofType
no
-
.
name Car
model
Classic Example Tramme
Type
co
--

SEC)
>
-
no
ofdoor

Stop (c)
- start
- Program to represent different types of vehicles
ACL)
- Represent entities like cars, motorcycles, etc.
Oo
-

00
- For cars, attributes like name, model; methods like start_engine(), stop_engine()
- Similar attributes and methods for the Motorcycle class
- Introduction of inheritance
Motorcycle -

F
- Create a superclass "Vehicles" name

suspen model handle


- Define general attributes and methods in the Vehicles superclass L
.
> -

Ty
L
e -
- Cars and Motorcycles, as subclasses, inherit from the Vehicles superclass SEL) Bayle
Stop (2)

no of Tyre Wheelied
lik
+ pub
- privateted
- Truck
# Protec
= xY2
H
-

--
k
+ And Type
main()
Vi
+
BigTankType
E vehicles

3
Copy#XT2 H xY 2
- .
Mode of Inheritance
1. What scope of attributes of parents will be inherited to child class and with what scope?
Protected Members

1. Members declared protected are accessible within class itself & to its derived class.
2. The protected access modifier serves two primary purposes:
1. Encapsulation: Like the private access modifier, protected provides a level of
encapsulation, ensuring that certain class members are not directly accessible from
outside the class. This promotes data hiding and prevents external code from modifying
or accessing sensitive data directly.
2. Inheritance: Unlike private, protected members can be inherited by derived classes. This
means that subclasses have limited access to these members, allowing them to build
upon the base class's functionality while maintaining some level of data integrity and
control.
Protected Members
Types of Inheritance
Examples
Advantages

1. Reusability: Avoids duplicating methods in child classes that already exist in parent classes.

2. Code modification: Localises changes, preventing inconsistencies throughout the program.

3. Extensibility: Allows easy enhancement or upgrade of specific parts of a product without


altering core attributes.

4. Data hiding: Supports encapsulation by keeping some data private in the base class,
preventing alteration by derived classes.
Implementing Encapsulation in C++
Implementing Encapsulation in Java
1. Java does not support multiple inheritance.

You might also like