abstraction and encapsulation

DecentGreen avatar
DecentGreen
·
·
Download

Start Quiz

Study Flashcards

24 Questions

What is the primary purpose of encapsulation in object-oriented programming?

To restrict accessing variables and methods directly

What is the type of access modifier that allows access to variables and methods within the class and its sub-classes?

Protected

What is the function of the __init__ method in the Employee class?

To initialize object properties

What is the output of the emp.show() method?

Name: Jessa Salary: 8000

What is the advantage of using public data members in a class?

They are accessible within and outside of a class

What is the type of access modifier that restricts access to variables and methods within the class?

Private

What is the purpose of the work method in the Employee class?

To print the employee's name and project

What is the benefit of using access modifiers in object-oriented programming?

To restrict accessing variables and methods directly

What is the primary purpose of the init method in the Car class?

To assign default values to the data attributes

What is the effect of calling the accelerate method on a Car object?

It increases the speed by 5

What is the purpose of the get_speed method in the Car class?

To return the current speed of the car

What is the data attribute in the Pet class that stores the type of animal?

_animal_type

What is the purpose of the set_name method in the Pet class?

To assign a value to the _name field

What is the difference between the accelerate and brake methods in the Car class?

The accelerate method increases the speed, while the brake method decreases the speed

What is the purpose of creating a Pet object?

To store information about a pet

What is the common characteristic of the data attributes in the Car and Pet classes?

They are all instance variables

What is the primary purpose of using getter and setter methods in a class?

To add validation logic for setting a value

What is the benefit of using private variables in a class?

It restricts access to the variables and allows modification only through setter methods

What is the purpose of the init method in a class?

To define a constructor for the class

What is the benefit of using information hiding in a class?

It restricts access to the variables and allows modification only through setter methods

What is the purpose of the setter method in a class?

To set the value of a private variable with validation

What is the benefit of using getter methods in a class?

It restricts access to private variables and allows retrieval only through getter methods

What is the purpose of using private variables in a class?

To restrict access to the variables and allow modification only through setter methods

What is the benefit of using conditional logic in setter methods?

It restricts access to private variables and allows modification only through setter methods with validation

Study Notes

Programming Exercise: Car Class

  • The Car class has three data attributes: _year_model, _make, and _speed.
  • The _init_ method initializes the car's year model, make, and sets the speed to 0.
  • The accelerate method adds 5 to the speed data attribute each time it is called.
  • The brake method subtracts 5 from the speed data attribute each time it is called.
  • The get_speed method returns the current speed of the car.

Programming Exercise: Pet Class

  • The Pet class has three data attributes: _name, _animal_type, and _age.
  • The _init_ method initializes the pet's name, animal type, and age.
  • The set_name, set_animal_type, and set_age methods assign values to the respective data attributes.
  • The get_name, get_animal_type, and get_age methods return the values of the respective data attributes.

Encapsulation and Abstraction

  • Encapsulation restricts access to variables and methods directly and prevents accidental data modification.
  • It is a way to restrict access to methods and variables from outside of a class.
  • Encapsulation is useful when working with sensitive data and providing access to all variables used within the class is not a good choice.

Access Modifiers

  • Python provides three types of access modifiers: public, private, and protected.
  • Public members are accessible anywhere from outside the class.
  • Private members are accessible within the class.
  • Protected members are accessible within the class and its sub-classes.

Information Hiding and Conditional Logic

  • Information hiding is used to restrict access to object attributes.
  • Conditional logic is used to add validation logic for setting an object attribute.
  • Private members can be used to restrict access to object attributes.
  • Setter methods can be used to modify private data members with rules.
  • Getter methods can be used to retrieve private data members.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser