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

Inheritance - Extending Class - 093906

Computer science

Uploaded by

gogoiabinash300
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Inheritance - Extending Class - 093906

Computer science

Uploaded by

gogoiabinash300
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

Lesson 06: INHERITANCE-Extending Class


Short Question/Answer
1. What is Inheritance?
 Inheritance is the capability of one class to inherit properties from another class.
2. What are the different form of Inheritance?
 Single inheritance, multiple inheritance, hierarchical inheritance, multilevel inheritance and hybrid
inheritance.
3. Define the followings -
 a) Single Inheritance : When a sub class inherits properties from only one base class. i.e.
one base class and one derived class, it is called single inheritance.
b) Multiple Inheritance : When a sub class inherits properties from multiple base classes i.e.
multiple base class and one derived class, it is called multiple
inheritance.
c) Hierarchical Inheritance : When many sub classes inherit properties from a single base class
i.e. one base class and multiple sub class, it is called hierarchical
inheritance.
d) Multilevel Inheritance : When a subclass inherits properties from a class that itself inherits
properties from another class, it is called single inheritance.
e) Hybrid Inheritance : When a sub class inherits properties from multiple base classes
and all of its base classes inherit properties from a single base
class, it is called hybrid inheritance.
Note: Block diagram is needed with definition
4. Which data members and member functions of a base class cannot be inherited by a derived class?
 The static data members and the member functions constructor and destructor of a base class cannot
be inherited by a derived class.
5. What is a base class?
 The class, whose properties are inherited, is known as a base class or super class.
6. What is a derived class?
 The class inheriting the properties of another class i.e., the base class, is known as derived class or
subclass.
7. State the relationship between the base and the derived class?
 The derived class has IS-A relationship with its base class.
8. What is -
 a) inheritance hierarchy : It refers to the relationship between a base class and derived class.
b) inheritance graph : It refers to the derivation from multiple base classes.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

9. What is access specifier? What is its significance?


 These are keywords that set the accessibility of classes, methods and other members.
It basically defines how the members of a class can be accessed in its derived class.
10. What do you mean by visibility mode? What is its significance?
 The visibility mode i.e., private, public or protected, is the access control of the inheritable members.
It basically controls the access of inheritable members of a base class in its derived class.
11. What is -
 a) IS-A relationship : It is the relationship between the classes, when a class inherits from
another class.
b) HAS-A relationship : It is the relationship between classes, when a class contains object
of other class-type as its member.
c) HOLDS-A relationship : It is the relationship between classes, when a class contains an object
of other class as its indirect member via pointer or reference.
12. How HAS-A relationship differs from HOLDS-A relationship?
 The class having HAS-A relationship with other class has the ownership of the contained object,
while, the ownership is missing in HOLDS-A relationship.
13. What are the factors on which the access of the inherited members depends?
 The access specifier of the base class member and its visibility mode in derived class.
14. Can the derived class can access the private members of a base class? Explain how it can be
accessed?
 No, the private members of a base class cannot be access directly by the derived class.
The derived class can access the private members of the base class indirectly only through the
inherited public member functions of the base class.
15. What do you mean by nested class?
 A class defined inside another class is called a nested class.

Conceptual Question/Answer
1. Why the concept of inheritance is introduced in OOPs? Or,
What is the importance of inheritance in context to OOPs? Or,
Discuss various reasons that support the concept of inheritance in Object Oriented Languages.
 Inheritance
i) Express the inheritable relationship of real-world models.
ii) Facilitates the code reusability.
iii) Simulates the transitive nature of real-world's objects thereby saves time and efforts on
modification.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

2. Briefly explain the visibility mode of a base class in a derived class.


 The private members of a base class are not all accessible to its derived class in any form of visibility
mode. However, if the accessibility mode is
i) private, all the private and protected members of the base class becomes the private members
of derived class.
ii) public, the private and protected members of the base class becomes the private and protected
members of derived class respectively.
iii) protected, all the private and protected members of the base class becomes the protected
members of the derived class.
3 Differentiate between public and protected visibility in context of OOPs.
 Public visibility Protected visibility
The public and protected members of the base Both public and protected members of the base
class is derived as public and protected itself. class is derived as protected members.
The public members of the base class gets visible The public members of the base class does not
for the objects of derived class. get visible for the objects of derived class.
4. How does inheritance influence the working of constructor and destructor?
 When an object of the derived class is declared, in order to create it, firstly the constructor of the
base class is invoked and then, the constructor of the derived class is invoked.
On the other hand, when an object of the derived class is destroyed, first the destructor of the
derived class is invoked followed by the destructor of the base class i.e., the destructors are invoked
in the reverse order of the constructor invocation.
5. How does the invocation of constructor differ in derivation of class and nesting of classes?
 In derivation of class first the base class constructor is invoked, followed by the derived class
constructor, whereas in nested classes constructors of all the member objects are called before the
constructors of the object enclosing other objects.
6. How Inheritance differs from containership?
 Inheritance facilitates to create or define a specialized instance of a class that shares the properties
of the class and at the same time adds new features to it.
Containership, on the other hand, facilitates to enclose objects of other classes inside a class. It is
just a way to define an object which itself is collection of objects of other classes.
7. How nested objects are created? Or, Illustrate a nested class.
 A nested object is created in two stages. First, the member objects are created using their respective
constructors, in the order they are defined in and then the other ordinary members of the nested
object are created. That is, the constructors of all the member objects are called before the
constructor of the object enclosing other objects.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

8. When does ambiguity arise in multiple inheritance? How can one resolve it?
 An ambiguity can arise when several paths exist to a class from the same base class. This means
that a child class could have duplicate sets of members inherited from a single base class. This can
be solved by using a virtual base class.
9. What is a virtual base class? What is its significance?
 When two or more objects are derived from a common base class, one can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class.
Virtual base class prevents the duplication of same object regardless of the number of paths that
exist to the child class.
10. How does the visibility mode control the access of members in the derived class? Explain with
examples.
 Visibility mode controls the access of member in derived class by using access specifier. Below table
shows different visibility modes.

*********************

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

You might also like