answersLogoWhite

0


Best Answer

In object-oriented programming, inheritance allows the creation of is-a relationships. For example, a car is a vehicle, and a bike is a vehicle, so those could be modeled through a vehicle class, and a pair of car and bike classes, both derived from (inheriting from) the vehicle class. Derived classes like car and bike share common vehicle properties, such as speed, location, direction, number of wheels, etc.

User Avatar

Wiki User

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

Wiki User

6y ago

Inheritance is useful whenever we need to work with a collection of objects of different types but where all those objects have something in common. Planes, trains and automobiles are different types of vehicle; cows, sheep and chickens are different types of farm animal; ellipses, rectangles and triangles are different types of shape.

Whenever objects of different type share something in common, we can combine those common properties within a (base) class from which we can inherit a more specific (derived) class of object. The derived class inherits all the public and protected properties of its base class and can therefore be treated just as if it really were that base class.

Classes intended to be used as base classes usually provide one or more virtual methods which can be overridden by derived classes to provide more specialised behaviour. This allows us to generalise our code; we do not need to know the exact runtime type of a derived object so long as we know one of its base classes. If we invoke a virtual method of the base class, the most-derived override of that method will be invoked. This is known as polymorphism; objects automatically behave according to their runtime type even when the runtime type is completely unknown to us.

Consider the following:

void draw (Vector<Shape*>& shapes) {

for (auto shape : shapes) shape->draw();

}

Here, we assume that Shape::draw() is a virtual method. Classes that inherit from Shape (such as Circle, Rectangle, Triangle, Hexagon, and so on), can override the Shape::draw() method in order to provide the exact method we expect of these specific shapes. Note how the code does not need to know the specific type of shape to draw; the shapes know how to draw themselves according to their runtime type, all we need do is invoke the method. Even if we knew all the types that inherit from shape, we may add new shapes at a future date and the code will still work without any modification.

Of course we could achieve the same thing without inheritance. Typically this would involve using a type field to differentiate between shapes:

enum Type {circle, triangle, rectangle, pentagon, hexagon };

class Shape {

private:

Type type;

public

void draw();

// ...

};

However, rather than allowing derived classes to provide the specific behaviours, we have to place all the behaviours in the shape class using thinly-veiled switches:

void Shape::draw() {

if (type==circle) // draw a circle

else if (type==triangle) // draw a triangle

else if (type==rectangle) // draw a rectangle

// and so on...

}

This means that whenever we add a new type of shape, we have to update the enum, and then update every class method within shape that uses the type field. That in itself increases the maintenance burden. It can also lead to errors if we forget to update every method -- of which there may be many. More importantly, selecting the appropriate method based on a type field has a far greater performance impact than invoking a virtual function.

Another problem is how we choose to represent every type of shape using just the one class. If we represent all the properties of every type of shape, we find that some properties apply to many shapes while others only apply to a few. A radius only applies to circles, for instance, but every shape would still need to have a radius property. With inheritance, we simply place all the properties (if any) that are common to all derived classes, and place specialised properties only in those classes that require them. In this way, only circles need have a radius property.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Inheritance allows you to save time and code by allowing you to pull the details from one class and allow it to overlap with the details of another class, all the while allowing both classes to stay separate and distinct. Let me give a really simple example: I have a class named Mammal. Mammals have distinct traits such as being born alive, producing milk, having hair, large brains, etc. Now I have a different class named Human. The class Human contains all of the traits of the class Mammal, but it also has details of its own, separate than that of Mammal, such as the ability to think intelligently, walk on two legs, etc.

Inheritance works in a IS_A relationship. For example, a Human IS_A Mammal, but the reverse is not true, not all Mammals are Humans.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Inheritance is a key feature of object oriented programming because:

  • It promotes code reuse
  • It avoids code redundancy

Java allows us to use Inheritance extensively in our programs.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

Because true inheritance was impossible to implement in a structural programing environment.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why the concept of inheritance was introduced in OOP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Object Oriented Programming without the principle of Polymorphism?

Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.


Basic concepts of OOP?

The four main pillars of all OOP languages are encapsulation, inheritance, polymorphism and abstraction.


In OOP the concept of insulating data and from direct access by the program is known as?

In OOP, the concept of insulating data and from direct access by the program is known as


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


What is need and characteristics of oop?

OOP stands for Object oriented programming. The main characteristics of OOP are 1. Class 2. Objects 3. Instance 4. Methods 5. Message Passing 6. Inheritance 7. Abstraction 8. Encapsulation 9. Polymorphism &amp; 10. Decoupling OOP is a concept that allows us to have a program that is 1. "robust and secure". 2. "architecture neutral and portable". 3. "high performance".

Related questions

Which concept in java implements the concept of reusabilityand what are it's types?

One important aspect of code reuse is related to inheritance, which is a standard part of OOP programming.


What is Object Oriented Programming without the principle of Polymorphism?

Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.Polymorphism is an important principle in OOP; it would be hard to imagine OOP without it. Other important principles are inheritance, and encapsulation.


Basic concepts of OOP?

The four main pillars of all OOP languages are encapsulation, inheritance, polymorphism and abstraction.


What are the benefits and application of OOP?

Encapsulation, inheritance, polymorphism and abstraction.


In OOP the concept of insulating data and from direct access by the program is known as?

In OOP, the concept of insulating data and from direct access by the program is known as


Why is C plus plus called an object oriented language?

It is called an OOP language because it supports the four pillars of the OOP paradigm: abstraction, encapsulation, inheritance and polymorphism. However, it is not 100% object oriented as it also supports the concept of primitive variables, including pointers, which are not implemented as objects.


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


What is need and characteristics of oop?

OOP stands for Object oriented programming. The main characteristics of OOP are 1. Class 2. Objects 3. Instance 4. Methods 5. Message Passing 6. Inheritance 7. Abstraction 8. Encapsulation 9. Polymorphism &amp; 10. Decoupling OOP is a concept that allows us to have a program that is 1. "robust and secure". 2. "architecture neutral and portable". 3. "high performance".


What are the concepts of object oriented programming in c plus plus?

The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.


How oops concept is implemented in java?

OOP stands for Object Oriented Programming. Everything in Java is an Object. Any class you create extends the Object class by default thereby making everything in Java an object. Moreover, you can use features like Inheritance, Polymorphism, Encapsulation etc which are OOP concepts thereby making Java an Object Oriented Programming Language


What are the differences between Java OOP and PHP OOP?

JAVA is an Object Based Programming Language. it doesn't provide multiple inheritance and operator overloading. while Object Oriented Lanuages provides both.


What are the reasons for rise of constructor concept in c?

Not sure what you mean by this. C is a not an object-oriented programming (OOP) language, and therefore has no constructor concept. You probably meant C++ but, even so, there is no "rise of constructor concept". Constructors are fundamental to OOP -- they allow you to initialise an object at the point of instantiation.