inheritance in oops

17
INHERITANCE IN OOPS Sharda University Department Of Computer Science And Engineering School Of Engineering And Technology Greater Noida, U. P.

Upload: hirra-sultan

Post on 10-Feb-2015

804 views

Category:

Software


1 download

DESCRIPTION

Describes inheritance in object oriented programming in general and in Java, C++, Python and ADA in particular

TRANSCRIPT

Page 1: Inheritance in oops

INHERITANCE IN OOPS

Sharda University

Department Of Computer Science And Engineering

School Of Engineering And Technology

Greater Noida, U. P.

Page 2: Inheritance in oops

Presented By Hirra Sultan

CSE-B 2nd yearRoll No. 120101091

Enrollment No. 2012017740Supervisor: Mr. A. K. Sahoo

[email protected]

Page 3: Inheritance in oops

Introduction

Background The first OOP language designed for the

first personal computer was smalltalk. When OOP was integrated into C

language, the resulting language was called C++ and it became the first object-oriented language to be widely used commercially.

Later java and other OOP languages were developed.

Page 4: Inheritance in oops

Concept of Inheritance Inheritance is that feature of an OOP language

which allows reusability of code of a class and is considered corner stone of OOP languages.

Using inheritance, we can create a general class that defines traits common to a set of related items.

This class may then be inherited by other, more specific classes, each adding only those things that are unique to the inheriting class.

Page 5: Inheritance in oops

Base class: The class which gets inherited is called a base class. The code of this class is passed on to subclasses where it is reused.

Derived class: A subclass is a derived class which inherits the base class and uses its member functions.

Un-inheritable class: A class may be declared as un-inheritable by adding certain class modifiers to the class declaration before the "class" keyword and the class identifier declaration. Such sealed classes restrict reusability.

Definitions

Page 6: Inheritance in oops

Types of Inheritance

Single Inheritance: In single inheritance there is only one super class and only one sub class.

Multi-level inheritance: In multi-level inheritance a derived class is inherited by another class thus making multiple levels.

Page 7: Inheritance in oops

Multiple Inheritance: A class can inherit the attributes of two or more classes. This is known as multiple inheritance.

Hierarchical inheritance: When a base class is inherited by multiple derived classes it is called hierarchical inheritance.

Hybrid inheritance: This is a mixture of two or more inheritances in a single code.

Page 8: Inheritance in oops

Inheritance may be derived in three forms which decides the way inherited data members can be used. Public Inheritance: Public members of

the base class become public members of the derived class and protected members of the base class become protected members of the derived class.

Page 9: Inheritance in oops

Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Page 10: Inheritance in oops

Inheritance in C++ In C++ all the five types of inheritances are

applicable. Friend functions and constructors can’t be

inherited. The general syntax of inheritance is:

class derived-class-name : visibility-mode base-class-name

{

…// members of derived class

};

Page 11: Inheritance in oops

Inheritance in Java The general syntax of inheritance is:

Class Subclass-name extends superclass-name

{

//methods and fields

}

Page 12: Inheritance in oops

The keyword extends indicates that we are making a new class that derives from an existing class.

Multiple and hybrid inheritance is not supported. This reduces the program complexity.

Constructors are not inherited by a subclass.

Page 13: Inheritance in oops

Inheritance in Python Instances inherit from classes, and classes

inherit from super classes. Python supports a limited form of multiple

inheritance. The syntax for inheritance in python is:

class DerivedClassname (BaseClassName):

<statement-1>

.

<statement-N>

Page 14: Inheritance in oops

Inheritance in ADA

In Ada 95 terminology, types that can have parents or children are termed “tagged types”, and have the keyword “tagged” as part of their definition.

If we don't redefine a subprogram for a given type, the closest ancestor's defined subprogram will be used.

Page 15: Inheritance in oops

Advantages

We save time because much of the code needed for our class is already written.

We can extend and revise a parent class without corrupting the existing parent class features.

Page 16: Inheritance in oops

Disadvantages

Removing or swapping out a superclass will usually break subclasses.

It's inflexible. Inheritance relationships generally can't

be altered at runtime.

Page 17: Inheritance in oops