How can you use a class 1's method in another class 2. Class 2 does not inherit class 1.

Recommended Answers

All 4 Replies

You do not do that. You have design fault.

Using inheritance methos we can use class 1's methods in class 2. I think so!!

One way to use class 1's methods in class 2 is to have a class 2 object aggregate a class 1 instance. It is a standard pattern in OOP, called composition, which is often used as an alternative to inheritance

class Class1(object):
    def method(self):
        print(self)


class Class2(object):
    def __init__(self):
        self.obj1 = Class1()

    def foo(self):
        self.obj1.method()

c2 = Class2()
c2.foo()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.