answersLogoWhite

0


Best Answer

Single Inheritance

In "single inheritance," a common form of inheritance, classes have only one base class. Consider the relationship illustrated in the following figure. Simple Single-Inheritance Graph

Note the progression from general to specific in the figure. Another common attribute found in the design of most class hierarchies is that the derived class has a "kind of" relationship with the base class. In the figure, a Book is a kind of a PrintedDocument, and a PaperbackBook is a kind of a book.

One other item of note in the figure: Book is both a derived class (from PrintedDocument) and a base class (PaperbackBook is derived from Book). A skeletal declaration of such a class hierarchy is shown in the following example:
Copy Code // deriv_SingleInheritance.cpp // compile with: /LD class PrintedDocument {}; // Book is derived from PrintedDocument. class Book : public PrintedDocument {}; // PaperbackBook is derived from Book. class PaperbackBook : public Book {};

PrintedDocument is considered a "direct base" class to Book; it is an "indirect base" class to PaperbackBook. The difference is that a direct base class appears in the base list of a class declaration and an indirect base does not.

The base class from which each class is derived is declared before the declaration of the derived class. It is not sufficient to provide a forward-referencing declaration for a base class; it must be a complete declaration.

In the preceding example, the access specifier public is used. The meaning of public, protected, and private inheritance is described in Member-Access Control.

A class can serve as the base class for many specific classes, as illustrated in the following figure. Sample of Directed Acyclic Graph

In the diagram shown above, called a "directed acyclic graph" (or "DAG"), some of the classes are base classes for more than one derived class. However, the reverse is not true: there is only one direct base class for any given derived class. The graph in the figure depicts a "single inheritance" structure. Note:

Directed acyclic graphs are not unique to single inheritance. They are also used to depict multiple-inheritance graphs. This topic is covered in Multiple Inheritance.

In inheritance, the derived class contains the members of the base class plus any new members you add. As a result, a derived class can refer to members of the base class (unless those members are redefined in the derived class). The scope-resolution operator (::) can be used to refer to members of direct or indirect base classes when those members have been redefined in the derived class. Consider this example:
Copy Code

// deriv_SingleInheritance2.cpp // compile with: /EHsc /c #include using namespace std; class Document { public: char *Name; // Document name. void PrintNameOf(); // Print name. }; // Implementation of PrintNameOf function from class Document. void Document::PrintNameOf() { cout << Name << endl; } class Book : public Document { public: Book( char *name, long pagecount ); private: long PageCount; }; // Constructor from class Book. Book::Book( char *name, long pagecount ) { Name = new char[ strlen( name ) + 1 ]; strcpy_s( Name, strlen(Name), name ); PageCount = pagecount; };

Note that the constructor for Book, (Book::Book), has access to the data member, Name. In a program, an object of type Book can be created and used as follows:
Copy Code

// Create a new object of type Book. This invokes the // constructor Book::Book. Book LibraryBook( "Programming Windows, 2nd Ed", 944 ); ... // Use PrintNameOf function inherited from class Document. LibraryBook.PrintNameOf();

As the preceding example demonstrates, class-member and inherited data and functions are used identically. If the implementation for class Book calls for a reimplementation of the PrintNameOf function, the function that belongs to the Document class can be called only by using the scope-resolution (::) operator:
Copy Code

// deriv_SingleInheritance3.cpp // compile with: /EHsc /LD #include using namespace std; class Document { public: char *Name; // Document name. void PrintNameOf() {} // Print name. }; class Book : public Document { Book( char *name, long pagecount ); void PrintNameOf(); long PageCount; }; void Book::PrintNameOf() { cout << "Name of book: "; Document::PrintNameOf(); }

Pointers and references to derived classes can be implicitly converted to pointers and references to their base classes if there is an accessible, unambiguous base class. The following code demonstrates this concept using pointers (the same principle applies to references):
Copy Code

// deriv_SingleInheritance4.cpp // compile with: /W3 struct Document { char *Name; void PrintNameOf() {} }; class PaperbackBook : public Document {}; int main() { Document * DocLib[10]; // Library of ten documents. for (int i = 0 ; i < 10 ; i++) DocLib[i] = new Document; }

In the preceding example, different types are created. However, because these types are all derived from the Document class, there is an implicit conversion to Document *. As a result, DocLib is a "heterogeneous list" (a list in which not all objects are of the same type) containing different kinds of objects.

Because the Document class has a PrintNameOf function, it can print the name of each book in the library, although it may omit some of the information specific to the type of document (page count for Book, number of bytes for HelpFile, and so on). Note:

Forcing the base class to implement a function such as PrintNameOf is often not the best design. Virtual Functions offers other design alternatives.

By:- Deb

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

1. Single Inheritance

A Scenario where one class is inheriting/extending the behavior of just one super class.

Ex: public class Ferrari extends Car {…}

This can be taken much further, where one class is inheriting/extending the bahavior of another class which in turn is inheriting behavior from yet another class.

Ex: public class Automobile {…}

Public class Car extends Automobile {…}

Public class Ferrari extends Car {…}

This multi-level inheritance actually has no limitations on the number of levels it can go. So as far as Java goes, it is limitless. But for maintenance and ease of use sakes it is better to keep the inheritance levels to a single digit number.

2. Multiple Inheritance

This type of inheritance allows for a class to inherit from more than one superclass. That is, a child class can have more than one parent.

Ex.

public class ArmoredCar extends Car, Tank

As with single inheritance, there can be many levels of multiple inheritance.

Unfortunately, multiple inheritance is much more complicated to properly implement than single inheritance, due to what is known as namespace collision. For instance, what if both the Car and the Tank classes above have a method Drive(). Which superclass method does ArmoredCar use, if it doesn't explicitly specify its own Drive() method? If it wants to extend one, how does it do that? In the context of the ArmoredCar class, what does the super() method refer to? Languages which attempt to allow multiple inheritance run into a large number of these problems, which require either compiler or language workarounds, making them both more verbose and more complex than languages which allow only single inheritance.

Do note that languages which support multiple inheritance do not require its use - programs in such languages can solely use single inheritance. In general, most OO languages suggest using multiple inheritance where very substantial savings in code and/or design logic can be realized over a single inheritance design.

Java does not support Multiple Inheritance. C++ is a common language which does allow Multiple Inheritance.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Single inheritance is when a child class is derived from only one parent class. Multiple inheritance is when a child class is derived from more than one parent class.

I should clarify that this is when the derivation is done at the same time. If you derive a child from a parent, and then derive a grandchild from the child, that is not multiple inheritance - that is sequential single inheritance.

Multiple inheritance can be tricky if there are like named members in both parents. Special syntax is used to ensure that a reference to such a member is properly qualified.

I seem to recall that Java does not support multiple inheritance.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Single Inheritance is a design wherein one class inherits or extends the features of another class. All the public features of the parent class are visible in the class that extends it.

Ex:

public class A extends B {

…..

}

Here class A is the child class and B is the parent class. This is single inheritance.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

it is a kuntham which contain only one class

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

single inheritance

A class derived from only one base class

multilevel inheritance

A class derived from another derived class

s.gunasekaran

VSBEC

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The Example of a single inheritance is:

deriving a class SON from base class FATHER...

class son:public father

{

...

...

...

};

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is single inheritance explain with an example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What is a unit inheritance?

Unit Inheritance or Single Inheritance refers to the situation where one class inherits/extends the features of another class ex: public class A extends B { ..... } The above is an example of unit inheritance.


What is single inheritance in oops?

Single Inheritance is the concept of deriving a class properties from a single base class


The inheritance of Marfan syndrome is an example of?

The "autosomal dominant" method of inheritance. if your question as Marfan syndrome is the result of inheriting a single allele. Individuals with Marfan syndrome are tall and long-limbed, and have both cardiovascular and eye defects. The inheritance of Marfan syndrome is an example of ______. then the answer is pleiotropy


Different types of inheritances?

Single Inheritance Multiple Inheritance Multilevel Inheritance


What is an inheritance Explain different types of inheritance in c?

C is not an object oriented language and therefore has no native support for inheritance.


Does hybrid inheritance consist of ANY two types of inheritance?

There are only two types of inheritance to begin with: single inheritance and multiple inheritance. Since they are mutually exclusive there is no such thing as hybrid inheritance.


Can explain sudden changes in inheritance patterns?

mutations


What are the advantages of multiple inheritance over single inheritance?

The advantages of multiple inheritance over single inheritance include being a realistic software model. It is useful in breaking down complicated behavior into sets of characteristics that does not interfere with one another.


Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


Can you give Practical example for multilevel Inheritance?

Grandfather-father-child relationship is an example for multi level inheritance..


Explain different types of inheritance of object oriented programming?

== ==


Demonstrate single inheritance in C plus plus?

struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };