Thursday, 20 April 2017

How to Using the properties of one class into another class

How to Using the properties of one class into another class:

We can use the properties of one class into another class in two ways. They are:
  • has-a relationship
  • is-a relationship or inheritance

has-a relationship:-

  • The concept of using the properties of one class into another class by using class name or by using reference variable name is known as a has-a relationship.

class x:
    a=1000
    def __init__(self):
        self.b=2000
    def m1(self):
        print("i am in m1")
    def m2(self):
        print("i am in m2")
class y:
    def display(self):
        print(x.a)
        x_obj=x( )
        print(x_obj.b)
        x_obj.m1( )
        x_obj.m2( )
y_obj=y( )
y_obj.display( )

1000
2000
i am in m1
i am in m2

class x:
    a=10
    def __init__(self):
   self.b=20
    def m1(self):
   print("in m1 of x")
class y:
    c=30
    def __init__(self):
   self.d=40
    def m2(self):
   print(x.a)
   x_obj=x( )
   print(x_obj.b)
   x_obj.m1( )
   print(y.c)
   print(self.d)
   print("in m2 of y")
y_obj=y( )
y_obj.m2( )

10
20
in m1 of x
30
40
in m2 of y

is-a relationship or inheritance:-

  • The concept of using the properties of one class into another class directly is known as inheritance or is-a relationship.
  • A class which is extended by other class is known as a super class or base class or Parent class.
  • A class which is extending another class is known as a sub class or child class or Derived class.
  • super class properties directly accessible to the sub class like as a sub class properties.

class x:
    a=10
    def m1(self):
   print("in m1 of x")
class y(x): 
    c=30
    def __init__(self):
   self.d=40
    def m2(self):
   print(y.a)
   self.m1()
   print(y.c)
   print(self.d)
   print("in m2 of y")
y_obj=y( )
y_obj.m2( )


10
in m1 of x
30
40
in m2 of y

Note:
  • super class properties and sub class properties can be accessed by using subclass name or by using sub class reference variable.

class x:
    a=10
    def m1(self):
   print("in m1 of x")
class y(x):
    c=30
    def __init__(self):
   self.d=40
    def m2(self):
   print("in m2 of y")
   print(y.a)
   print(y.c)
y_obj=y( )
print(y_obj.d)
y_obj.m1( )
y_obj.m2( )

40
in m1 of x
in m2 of y
10
30

class x:
    def __init__(self):
   print("Constructor of x")
    def m1(self):
   print("in m1 of X")
class y(x):
    def m2(self):
   print("in m2 of y")
y_obj=y( )
y_obj.m1( )
y_obj.m2( )

Constructor of x
in m1 of X
in m2 of y

Object:-
  • It is a pre-defined class, which is defined built- in module
  • object class is a super class or base class for every class in python.
  • Object class properties can be accessed directly in every class or we can access through any class reference variable.

class x:
    def m1(self):
   print("in m1 of X")
print(x.__bases__)
x_obj=x( )
print(x_obj)

(<class 'object'>,)
<__main__.x object at 0x00000000034D1E10>

class a:
    pass
print(a.__bases__)
print(dir(object))
help(object)
help(object.__hash__)

Types of Inheritances:-

  • Single Inheritance
  • Multi-level Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Cyclic Inheritance

Single Inheritance:-
  • Concept of inheriting the properties from only one class into another class is known as single inheritance.

class X(object):
    """Single Inheritance"""
    def m1(self):
   print("in m1 of X")
x1=X( )
x1.m1( )

in m1 of X

Multi-level Inheritance:-
  • The concept of inheriting the properties from multiple classes into single class with concept of one after another is known as multi-level inheritance.

class X(object):
    """Multi-level Inheritance"""
    def m1(self):
   print("in m1 of X")
class Y(X):
    def m2(self):
   print("in m2 of Y")
class Z(Y):
    def m3(self):
   print("in m3 of Z")
z1=Z( )
z1.m1( )
z1.m2( )
z1.m3( )

in m1 of X
in m2 of Y
in m3 of Z

Multiple Inheritance:-
  • Concept of inheriting the properties from multiple classes into single class at a time is known as multiple inheritance.

class X(object):
    """Multiple Inheritance"""
    def m1(self):
   print("in m1 of X")
class Y:
    def m2(self):
   print("in m2 of Y")
class Z(Y,X):
    def m3(self):
   print("in m3 of Z")
z1=Z( )
z1.m1( )
z1.m2( )
z1.m3( )

in m1 of X
in m2 of Y
in m3 of Z

Hierarchical Inheritance:-
  • The concept of inheriting the properties from one class into multiple classes is known as hierarchical inheritance.

class X(object):
    """Hierarchical Inheritance"""
    def m1(self):
   print("in m1 of X")
class Y(X):
    def m2(self):
   print("in m2 of Y")
class Z(X):
def m3(self):
   print("in m3 of Z")
y1=Y( )
y1.m1( )
y1.m2( )
z1=Z( )
z1.m1( )
z1.m3( )

in m1 of X
in m2 of Y
in m1 of X
in m3 of Z

Cyclic Inheritance:-
  • The concept of inheriting the properties from sub class to super class is known as cyclic inheritance.

Note:-
  • Python does not support cyclic inheritance.
  • in this case object class , object not created,there is no hash code generated.
  • python interprter only takes the hash code value,because of that reason,python not supported cyclic inheritance.

class X(Z):
    """Cyclic  Inheritance is not possible in Python"""
    def m1(self):
   print("in m1 of X")
class Y(X):
    def m2(self):
   print("in m2 of Y")
class Z(Y):
    def m3(self):
   print("in m3 of Z")

z1=Z( )
z1.m1( )
z1.m2( )
z1.m3( )


Traceback (most recent call last):
  File "E:/python_practice/examples/cyclic inhritance.py", line 1, in <module>
    class X(Z):
NameError: name 'Z' is not defined

why the object class is the super class for all classes in python ?

  • python interpreter generates the hash-code(indirect address) based on the original address of the object.
  • after creating the object the original address of the object is taken by the python interpreter,generates the hash code values based on the original address of the object by calling                                       _ _hash_ _( )
  • _ _hash_ _( ) method is present in side the object class because of that reason object class make it has a super class for every class in python.
class a:
    def m1(self):
        print("in m1 of a")
class b(a):
    def m2(self):
        print("in m2 of b")
b_obj=b()
print(b_obj)
print(b_obj.__hash__)           

<__main__.b object at 0x00000000034DC438>

<method-wrapper '__hash__' of b object at 0x00000000034DC438>   

No comments:

Post a Comment