ENCAPSULATION IN PYTHON

GOCOURSE



Encapsulation

The practice of keeping fields within a class private, then providing access to those fields via public methods. Encapsulation is a protective barrier that keeps the data and code safe within the class itself. We can then reuse objects like code components or variables without allowing open access to the data system-wide. Binding (or wrapping) code and data together into a single unit are known as encapsulation.

Program:


  class company:
     def __init__(self, name, salary, working_days):
        self.name = name
        self.salary = salary
        self.working_days = working_days

     def demofunc(self):
        print("worker name "+self.name)
        print("salary amount Rs",+self.salary)
        print("working days",+self.working_days)

  c1 = company("Steve",10000, 30)
  c2 = company("Chris",9000, 10)
  c3 = company("Mark",20000, 65)
  c4 = company("Kate",6000, 5)

  c1.demofunc()
  c2.demofunc()
  c3.demofunc()
  c4.demofunc()


Output:


  worker name Steve
  salary amount Rs 10000
  working days 30
  worker name Chris
  salary amount Rs 9000
  working days 10
  worker name Mark
  salary amount Rs 20000
  working days 65
  worker name Kate
  salary amount Rs 6000
  working days 5





More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send