Professional Documents
Culture Documents
Inheritance
Inheritance
Inheritance
Ineritance
Inheritance
In object-oriented programming, inheritance is
the mechanism of basing an object or class
upon another object (prototype-based
inheritance) or class (class-based inheritance),
retaining similar implementation.
History
Inheritance was invented in 1969 for Simula
and is now used throughout many object-
oriented programming languages such as Java,
C++, PHP,C#,Kotlin and Python.
Child-Parent
Defined as deriving new classes (sub classes) from
existing ones such as super class or base class and
then forming them into a hierarchy of classes.
In most class-based object-oriented languages, an object
created through inheritance, a "child object", acquires all
the properties and behaviors of the "parent object" , with
the exception of: constructors, destructor, overloaded
operators and friend functions of the base class.
Types of inheritance
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
Single inheritance
This is a form of inheritance in which a class
inherits only one parent class. This is the simple
form of inheritance and hence also referred to
as simple inheritance.
Single inheritance
class Parent:
void f1(self):
print("Function of parent class.")
class Child(Parent):
void f2(self):
print("Function of child class.")
object1 = Child()
object1.f1()
object1.f2()
Multiple Inheritance
An inheritance becomes multiple inheritances when
a class inherits more than one parent class. The
child class after inheriting properties from various
parent classes has access to all of their objects.
Not all programming languages support multiple
inheritance, sometimes it is implemented through
interfaces.
Multiple Inheritance
class Parent_1:
void f1(self):
print("Function of parent_1 class.")
class Parent_2:
void f2(self):
print("Function of parent_2 class.")
class Parent_3:
void f3(self):
print("function of parent_3 class.")
class Child(Parent_1, Parent_2, Parent_3):
void f4(self):
print("Function of child class.")
object_1 = Child()
object_1.f1()
object_1.f2()
object_1.f3()
object_1.f4()
Multi-level Inheritance
Inheritance where a subclass is inherited from
another subclass. This process can be
extended to any number of levels.
Multi-level Inheritance
class Parent:
void f1(self):
print("Function of parent class.")
class Child_1(Parent):
void f2(self):
print("Function of child_1 class.")
class Child_2(Child_1):
void f3(self):
print("Function of child_2 class.")
obj_1 = Child_1()
obj_2 = Child_2()
obj_1.f1()
obj_1.f2()
print("\n")
obj_2.f1()
obj_2.f2()
obj_2.f3()
Hierarchical inheritance
In this, various Child classes inherit a single
Parent class.
Hierarchical inheritance
class Parent:
deff1(self):
print("Function of parent class.")
class Child_1(Parent):
deff2(self):
print("Function of child_1 class.")
class Child_2(Parent):
deff3(self):
print("Function of child_2 class.")
obj_1 = Child_1()
obj_2 = Child_2()
obj_1.f1()
obj_1.f2()
print('\n')
obj_2.f1()
obj_2.f3()
Hybrid Inheritance
When there is a combination of more than one
form of inheritance, it is known as hybrid
inheritance.
Hybrid Inheritance
class Parent:
def f1(self):
print("Function of parent class.")
class Child_1(Parent):
def f2(self):
print("Function of child_1 class.")
class Child_2(Parent):
def f3(self):
print("Function of child_2 class.")
class Child_3(Child_1, Child_2):
def f4(self):
print("Function of child_3 class.")
obj = Child_3()
obj.f1()
obj.f2()
obj.f3()
obj.f4()
Overriding
Many object-oriented programming languages permit a class or object
to replace the implementation of an aspect—typically a behavior—that
it has inherited.
This process is called overriding. Overriding introduces a complication:
which version of the behavior does an instance of the inherited class
use—the one that is part of its own class, or the one from the parent
(base) class?
The answer varies between programming languages, and some
languages provide the ability to indicate that a particular behavior is not
to be overridden and should behave as defined by the base class.
Overriding
class Parent:
def f1(self):
print("Function of Parent class.")
class Child(Parent):
def f1(self):
print("Function of Child class.")
obj = Child()
obj.f1()
The super() function
The super() function in returns a proxy object
that references the parent class using the super
keyword. This super() keyword is basically
useful in accessing the overridden methods of
the parent class.
The super() function
class Parent:
def f1(self):
print("Function of Parent class.")
class Child(Parent):
def f1(self):
super().f1()
print("Function of Child class.")
obj = Child()
obj.f1()
Non-subclassable classes
In some languages a class may be declared as non-subclassable
by adding certain class modifiers to the class declaration.
Examples include the final keyword in Java and C++11 onwards or
the sealed keyword in C#. Such modifiers are added to the class
declaration before the class keyword and the class identifier
declaration.
Such non-subclassable classes restrict reusability, particularly
when developers only have access to precompiled binaries and not
source code.
Non-overridable methods
Just as classes may be non-subclassable, method declarations
may contain method modifiers that prevent the method from
being overridden (i.e. replaced with a new function with the
same name and type signature in a subclass).
A private method is un-overridable simply because it is not
accessible by classes other than the class it is a member
function of (this is not true for C++, though). A final method in
Java, a sealed method in C# or a frozen feature in Eiffel cannot
be overridden.
Virtual methods
If the superclass method is a virtual method, then invocations of
the superclass method will be dynamically dispatched.
Some languages require that methods be specifically declared
as virtual (e.g. C++), and in others, all methods are virtual (e.g.
Java).
An invocation of a non-virtual method will always be statically
dispatched (i.e. the address of the function call is determined at
compile-time). Static dispatch is faster than dynamic dispatch
and allows optimizations such as inline expansion.
Visibility of inherited members
Not visible:
private
Visible:
protected
public
Code reuse
Implementation inheritance is the mechanism
whereby a subclass re-uses code in a base
class. By default the subclass retains all of the
operations of the base class, but the subclass
may override some or all operations, replacing
the base-class implementation with its own.